灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:1917回复:0

android学习笔记18--------UDP示例

楼主#
更多 发布于:2012-09-06 13:43


参考别人一幅原理图。








import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPClient
{
    private DatagramSocket client=null;
    private DatagramPacket packet=null;
    private InetAddress host=null;
    public static int portSend=8888;
    public static String name="192.168.142.199";
    
    
    public UDPClient()
    {        
        try
        {
            host = InetAddress.getByName(name);
            client=new DatagramSocket();
                                        
            System.out.println("客户端准备就绪");
        } catch (Exception e)
        {
            e.printStackTrace();
        }                            
    }
    
    public void UDPClientRun(byte[] buffer) throws IOException  
    {                                
        host = InetAddress.getByName(name);
        packet=new DatagramPacket(buffer,buffer.length, host, portSend);
        client.send(packet);    
        System.out.println("客户端发送消息");
    }
    
    public void UDPClientStop()
    {
        client.close();
    }
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPClient
{
private DatagramSocket client=null;
private DatagramPacket packet=null;
private InetAddress host=null;
public static int portSend=8888;
public static String name="192.168.142.199";


public UDPClient()
{  
  try
  {
   host = InetAddress.getByName(name);
   client=new DatagramSocket();
          
   System.out.println("客户端准备就绪");
  } catch (Exception e)
  {
   e.printStackTrace();
  }        
}

public void UDPClientRun(byte[] buffer) throws IOException
{        
  host = InetAddress.getByName(name);
  packet=new DatagramPacket(buffer,buffer.length, host, portSend);
  client.send(packet);
  System.out.println("客户端发送消息");
}

public void UDPClientStop()
{
  client.close();
}
}

[java]
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPService
{
    private DatagramSocket server =null;
    private DatagramPacket dPacket=null;
    private byte[] buffer = new byte[640];  
    public static int portServer=8888;
    
    public UDPService ()
    {
        try
        {
            server  = new DatagramSocket(portServer);    
            dPacket = new DatagramPacket(buffer, buffer.length);
        } catch (Exception e)
        {
            e.printStackTrace();
        }            
    }
    
    public byte[] UDPServiceRun()
    {                                                                                            
        try
        {
            server.receive(dPacket);
            System.out.println("服务端接收到消息");
        } catch (IOException e)
        {
            e.printStackTrace();
            System.out.println("服务端未收到消息");
        }                            
        return buffer;                      
    }
    
    public void UDPSeverStop()
    {
        server.close();
    }
    
    public String getClientName()
    {
        String name="";
        name=dPacket.getAddress().toString();
        return name;
    }
    
}


喜欢0 评分0
游客

返回顶部