1,UDP通信的程序组成:Socket
- 发送端:DatagramSocket();
1. 创建发送端;**DatagramSocket**
DatagramSocket ds = new DatagramSocket();
2. 创建一个数据包;**DatagramPacket**![image.png](https://cdn.nlark.com/yuque/0/2022/png/25975946/1645492317122-942d06aa-7b8c-4119-b65a-d641cd52d496.png#clientId=u04b78c47-6df4-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=100&id=ud883e91f&margin=%5Bobject%20Object%5D&name=image.png&originHeight=100&originWidth=1083&originalType=binary&ratio=1&rotation=0&showTitle=false&size=29118&status=done&style=none&taskId=uff3ddab7-6057-4c06-98d2-f4856f4016b&title=&width=1083)
byte[] bytes = "nh".getBytes();
DatagramPacket dp = new DatagramPacket(bytes, 0, bytes.length, InetAddress.getLocalHost(), 8083);
3. 发送数据包;
ds.send(dp);
//
ds.close();
接受端:DatagramSocket(int port);