简体   繁体   English

DatagramSocket.receive() 被多次调用

[英]DatagramSocket.receive() is called more than once

I am trying to receive a packet via UDP protocol.我正在尝试通过 UDP 协议接收数据包。

try {
    datagramSocket = new DatagramSocket(SOCKET_PORT);
    datagramSocket.setBroadcast(true);
    while (true) {
       byte[] receiveData = new byte[1024];
       DatagramPacket packet = new DatagramPacket(receiveData, 
       receiveData.length);
       datagramSocket.receive(packet);
       //...
    }
} catch (Exception ex) {
    ex.printStackTrace(); 
}

When I send a packet from client receive() method is returned but on the next loop it is not blocked waiting for the next packet.当我从客户端发送一个数据包时, receive()方法被返回,但在下一个循环中它不会被阻止等待下一个数据包。 It returns the same packet infinitely, but the client does not send any more packets.它无限地返回相同的数据包,但客户端不再发送任何数据包。

The problem was with specific emulator network bug.问题在于特定的模拟器网络错误。 On real device everything works fine在真实设备上一切正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM