简略的udp服务端和客户端

简单的udp服务端和客户端
1.服务端的代码。

  1. package udp; 
  2.  
  3. import java.io.IOException; 
  4. import java.net.DatagramPacket; 
  5. import java.net.DatagramSocket; 
  6. import java.net.InetSocketAddress; 
  7.  
  8. /**
  9. * UDP服务类
  10. */ 
  11. public class UdpServerSocket { 
  12.      
  13.     private byte[] buffer = new byte[1024]; 
  14.     private static DatagramSocket ds = null
  15.     private DatagramPacket packet = null
  16.     private InetSocketAddress socketAddress = null
  17.      
  18.     /**
  19.      * 测试方法
  20.      */ 
  21.     public static void main(String[] args) throws Exception { 
  22.         String serverHost = "127.0.0.1"; 
  23.         int serverPort = 3344; 
  24.         UdpServerSocket udpServerSocket = new UdpServerSocket(serverHost, 
  25.                 serverPort); 
  26.         while (true) { 
  27.             udpServerSocket.receive(); 
  28.             udpServerSocket.response("你好,吃了吗!"); 
  29.         }        
  30.     } 
  31.  
  32.     /**
  33.      * 构造函数,绑定主机和端口
  34.      */ 
  35.     public UdpServerSocket(String host, int port) throws Exception { 
  36.         socketAddress = new InetSocketAddress(host, port); 
  37.         ds = new DatagramSocket(socketAddress); 
  38.         System.out.println("服务端启动!"); 
  39.     } 
  40.  
  41.     /**
  42.      * 接收数据包,该方法会造成线程阻塞
  43.      */ 
  44.     public final String receive() throws IOException { 
  45.         packet = new DatagramPacket(buffer, buffer.length); 
  46.         ds.receive(packet); 
  47.         String info = new String(packet.getData(), 0, packet.getLength()); 
  48.         System.out.println("接收信息:" + info); 
  49.         return info; 
  50.     } 
  51.  
  52.     /**
  53.      * 将响应包发送给请求端
  54.      */ 
  55.     public final void response(String info) throws IOException { 
  56.         System.out.println("客户端地址 : " + packet.getAddress().getHostAddress() 
  57.                 + ",端口:" + packet.getPort()); 
  58.         DatagramPacket dp = new DatagramPacket(buffer, buffer.length, packet 
  59.                 .getAddress(), packet.getPort()); 
  60.         dp.setData(info.getBytes()); 
  61.         ds.send(dp); 
  62.     } 
  63. 2.客户端
  64. 1.package udp; 
    2. 
    3.import java.io.*; 
    4.import java.net.*; 
    5. 
    6./**
    7. * UDP客户端程序,用于对服务端发送数据,并接收服务端的回应信息
    8. */ 
    9.public class UdpClientSocket { 
    10.    private byte[] buffer = new byte[1024]; 
    11. 
    12.    private static DatagramSocket ds = null; 
    13.     
    14.    /**
    15.     * 测试客户端发包和接收回应信息的方法
    16.     */ 
    17.    public static void main(String[] args) throws Exception { 
    18.        UdpClientSocket client = new UdpClientSocket(); 
    19.        String serverHost = "127.0.0.1"; 
    20.        int serverPort = 3344; 
    21.        client.send(serverHost, serverPort, ("你好,亲爱的!").getBytes()); 
    22.        byte[] bt = client.receive(); 
    23.        System.out.println("服务端回应数据:" + new String(bt)); 
    24.        // 关闭连接 
    25.        try { 
    26.            ds.close(); 
    27.        } catch (Exception ex) { 
    28.            ex.printStackTrace(); 
    29.        } 
    30.    } 
    31. 
    32.    /**
    33.     * 构造函数,创建UDP客户端
    34.     */ 
    35.    public UdpClientSocket() throws Exception { 
    36.        ds = new DatagramSocket(8899); // 邦定本地端口作为客户端 
    37.    } 
    38.     
    39.    /**
    40.     * 向指定的服务端发送数据信息
    41.     */ 
    42.    public final void send(final String host, final int port, 
    43.            final byte[] bytes) throws IOException { 
    44.        DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(host), port); 
    45.        ds.send(dp); 
    46.    } 
    47. 
    48.    /**
    49.     * 接收从指定的服务端发回的数据
    50.     */ 
    51.    public final byte[] receive() 
    52.            throws Exception { 
    53.        DatagramPacket dp = new DatagramPacket(buffer, buffer.length); 
    54.        ds.receive(dp);      
    55.        byte[] data = new byte[dp.getLength()]; 
    56.        System.arraycopy(dp.getData(), 0, data, 0, dp.getLength());      
    57.        return data; 
    58.    } 
    59.}


1 楼 1320438999 2012-06-22  
这样看代码好累的,楼主可以放在代码块里面嘛~简略的udp服务端和客户端
2 楼 yangsongjing 5 小时前  
1320438999 写道
这样看代码好累的,楼主可以放在代码块里面嘛~简略的udp服务端和客户端

嗯 嗯。