服务端代码

1 public class UDPChatServer {
 2     //通讯端口
 3     private Integer port=8000;
 4     //数据报文的通讯通道对象
 5     private DatagramChannel channel;
 6     //发送数据的最大大小
 7     private final Integer MAX_SIZE=1024;
 8     
 9     private Charset charset=Charset.forName("UTF-8");
10 
11     public UDPChatServer() throws IOException {
12         //DatagramChannel本身的open方法返回一个DatagramChannel对象
13         channel=DatagramChannel.open();
14         //获取数据报的socket对象UDP 的socket
15         DatagramSocket socket=channel.socket();
16         //将socket对象绑定上本地的端口
17         SocketAddress address=new InetSocketAddress(port);
18         socket.bind(address);
19         System.out.println("server started!");
20     }
21     private void service(){
22         //初始化一个新的字节缓存对象容量为MAX_SIZE=1024 个字节。
23         ByteBuffer receiveBuffer=ByteBuffer.allocate(MAX_SIZE);
24         while (true){
25               //将缓存中的数据清空
26             receiveBuffer.clear();
27             try {
28                 //从渠道中获取固定大小的数据,获取client连接
29                InetSocketAddress client= (InetSocketAddress) channel.receive(receiveBuffer);
30                //客户端每次请求到receiveBuffer里
31                 //设置buffer指针的下标0。这样就可以从buffer开头,对该buffer进行遍历(读取)了
32                 receiveBuffer.flip();
33                 //将字节数据转为字符串 
34                 String msg= charset.decode(receiveBuffer).toString();
35                 System.out.println(String.format("new data :%s %s %s ",client.getAddress(),client.getPort(),msg));
36                 //应答client 发送数据到通道中的客户端
37                 channel.send(ByteBuffer.wrap(reply(msg).getBytes(charset)),client);
38             } catch (IOException e) {
39                 e.printStackTrace();
40             }
41         }
42     }
43     private String reply(String word){
44         return "you said :"+word;
45     }
46     public static void main(String[] args) throws IOException {
47             new UDPChatServer().service();
48     }
49 }

客户端代码:

1 public class UDPChatClient {
 2     private Integer port=8000;
 3     private String host="localhost";
 4     //UDP socket
 5     private DatagramSocket socket;
 6     private InetAddress address;
 7     private Charset charset=Charset.forName("UTF-8");
 8     
 9     public UDPChatClient() throws IOException {
10         socket=new DatagramSocket();
11         //设置服务器的地址
12         address=InetAddress.getByName(host);
13         System.out.println("connection ok");
14     }
15     public static void main(String[] args) throws IOException {
16         new UDPChatClient().say();
17     }
18     private void say() throws IOException {
19         BufferedReader localreader=new BufferedReader(new InputStreamReader(System.in));
20 
21         String word;
22         while ((word=localreader.readLine())!=null){
23             // 将string转为byte数组 发送udp包
24             byte[] data=word.getBytes(charset);
25             //组装包指定ip和端口的数据包
26             DatagramPacket outputPacket=new DatagramPacket(data,data.length,address,port);
27             //发送
28             socket.send(outputPacket);
29             //接收udp包放到byte数组中
30             DatagramPacket inputPacket=new DatagramPacket(new byte[1024],1024);
31             socket.receive(inputPacket);
32             //打印获取的数据
33             System.out.println(new String(inputPacket.getData(),0,inputPacket.getLength(),charset));
34             
35             //客户端主动关闭
36             if (word.equals("bye")){
37                 socket.close();
38                 break;
39             }
40             
41         }
42     }
43 }

 

作者:SummerChill

本博客为自己总结亦或在网上发现的技术博文的转载。 如果文中有什么错误,欢迎指出。以免更多的人被误导。