我想你忽略了一点,这段代码侦听端口5555:
在下面的代码的istruction packet.getPort()返回的端口号远程主机,该数据报正被发送到该远程主机或接收数据报的远程主机。
int MYPORT = 5555;
DatagramSocket dsocket = new DatagramSocket(MYPORT);
byte[] buffer = new byte[2048];
// Create a packet to receive data into the buffer
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while (true) {
// Wait to receive a datagram
dsocket.receive(packet);
// Convert the contents to a string, and display them
String msg = new String(buffer, 0, packet.getLength());
System.out.println(packet.getAddress().getHostName() + ": "
+ msg);
// Reset the length of the packet before reusing it.
packet.setLength(buffer.length);
System.out.printf(" using port %d\n", packet.getPort());
}
我有双重检查本地:
sudo lsof -iUDP -n -P | grep 5555
java 1606 freedev 5u IPv6 0x9ed7290ce134656f 0t0 UDP *:5555