我有以下两个程序:
服务器:
final DatagramSocket datagramSocket = new DatagramSocket(null);
datagramSocket.setReuseAddress(true);
datagramSocket.bind(new InetSocketAddress(12345));
System.out.println(datagramSocket.getReuseAddress()); // returns true
final byte[] buffer = new byte[4];
final DatagramPacket datagramPacket = new DatagramPacket(buffer, buffer.length);
datagramSocket.receive(datagramPacket);
System.out.println(new String(buffer));
datagramSocket.close();
客户端:
final byte[] data = "Test".getBytes();
final DatagramSocket datagramSocket = new DatagramSocket(null);
final DatagramPacket datagramPacket
= new DatagramPacket(data, data.length, InetAddress.getByName("localhost"), 12345);
datagramSocket.send(datagramPacket);
datagramSocket.close();
现在我在同一台机器上多次运行服务器并运行一次客户端。然后只有一个服务器收到该消息,但其他服务器不接收。
是否有可能所有服务器同时收到消息?
我的代码中是错误还是我对UDP重用的理解有误?