我正在尝试从UDP服务器获取数据。我已经将客户端udp套接字用于从UDP服务器接收数据。完全可以通过wire-shark获取数据,但是udp.receive(package)无法获取数据。
我已经尝试过不同的IP,例如192.168.0.252是鲨鱼和我的本地IP(127.0.0.1)中的源。我也尝试过不带界面。
NetworkInterface nif = NetworkInterface.getByName("enp2s0");
if(nif==null){
System.err.println("Error getting the Network Interface");
return;
}
System.out.println("Preparing to using the interface: "+nif.getName());
Enumeration<InetAddress> nifAddresses = nif.getInetAddresses();
InetSocketAddress inetAddr= new InetSocketAddress("127.0.0.1",6723);
//socket.bind(new InetSocketAddress(nifAddresses.nextElement(), 0));
this.udpSocket = new DatagramSocket(inetAddr);
System.out.println("Interface setted");
//直到此代码被完美初始化
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
// blocks until a packet is received
udpSocket.receive(packet);
//Data stucking here
msg = new String(packet.getData()).trim();
System.out.println(
"Message from " + packet.getAddress().getHostAddress() + ": " + msg);
代码保持运行状态,但不接收数据