1. 了解UDP
无连接,不可靠通信,但是通信效率高
应用场景:语音通话、视频直播
2. API
构造器 | 说明 |
---|---|
public DatagramSocket() | 创建客户端的Socket对象,系统会随机分配一个端口号 |
public DatagramSocket(int port) | 创建服务端的Socket对象,并指定端口号 |
public DatagramPacket(byte[] buf, int length, InetAddress address, int port) | 创建发送出去的数据包对象 |
public DatagramPacket(byte[] buf, int length) | 创建用来接收数据的数据包 |
方法 | 说明 |
---|---|
public void send(DatagramPacket datagramPacket) | 发送数据包 |
public void receive(DatagramPacket datagramPacket) | 使数据包接收数据 |
public int getLength() | 获取数据包,实际接收到的字节个数 |
3. 单发单收
- Client
public class Test2_Client {
public static void main(String[] args) throws Exception {
// 创建客户端对象
DatagramSocket datagramSocket = new DatagramSocket();
// 创建数据包对象
byte[] buf = "我是快乐的客户端,哈哈哈!!!".getBytes();
DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length, InetAddress.getLocalHost(), 8888);
// 发送数据
datagramSocket.send(datagramPacket);
System.out.println("客户端数发送完毕!!!");
// 关闭通信管道
datagramSocket.close();
}
}
- Server
public class Test2_Server {
public static void main(String[] args) throws Exception {
// 创建客户端对象
DatagramSocket datagramSocket = new DatagramSocket(8888);
// 创建接收数据包【因为一包数据是64KB】
byte[] buf = new byte[1024 * 64];
DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
// 接收数据
datagramSocket.receive(datagramPacket);
// 打印接收数据
String s = new String(buf, 0, datagramPacket.getLength());
System.out.println(s);
// 获取客户端详情
System.out.println(datagramPacket.getAddress().getHostAddress()); // 客户端ID
System.out.println(datagramPacket.getPort()); // 客户端端口
// 释放资源
datagramSocket.close();
}
}
datagramPacket.getAddress().getHostAddress(); // 客户端ID
datagramPacket.getPort(); // 客户端端口
注意:发送或者接收完数据之后一定要释放资源。
datagramSocket.close();
4. 多发多收【多客户端对应一个服务器】
- Client1
public class Test3_Client1 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(7777);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("输入发送内容:");
String command = scanner.nextLine();
if ("exit".equals(command)) {
socket.close();
System.out.println("欢迎下次使用,退出成功!!!");
break;
}
byte[] buf = command.getBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length, InetAddress.getLocalHost(), 6666);
socket.send(packet);
}
}
}
- Client2
public class Test3_Client2 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(8888);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("输入发送内容:");
String command = scanner.nextLine();
if ("exit".equals(command)) {
socket.close();
System.out.println("欢迎下次使用,退出成功!!!");
break;
}
byte[] buf = command.getBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length, InetAddress.getLocalHost(), 6666);
socket.send(packet);
}
}
}
- Service
public class Test3_Server {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(6666);
byte[] buf = new byte[1024 * 64];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
while (true) {
socket.receive(packet);
String s = new String(buf, 0, packet.getLength());
System.out.println(s);
System.out.println("------消息来自于:" + packet.getAddress().getHostAddress() + ":" + packet.getPort() + "------");
}
}
}
注意事项:
- 多个客户端的端口号不能一致,否则会出现端口占用的问题
- 如果IDEA要打开多个客户端需要进行配置