简体   繁体   English

Java DatagramSocket在繁重的网络负载下停止响应

[英]Java DatagramSocket stops responding under heavy network load

I have a client which needs to listen on a particular port for incoming UDP broadcast messages. 我有一个客户端,需要在特定端口上侦听传入的UDP广播消息。 I accomplish this by initializing a DatagramSocket object bound to the port with a setSoTimeout of 1500ms. 我通过使用setSoTimeout为1500ms初始化绑定到端口的DatagramSocket对象来完成此操作。

while (true) {
    try{
        DatagramSocket datagramSocket = new DatagramSocket(PORT);

        byte[] buffer = new byte[BUFF_LEN];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

        datagramSocket.receive(packet);

        [handoff packet to internal buffer to await processing]

    } catch (SocketTimeoutException timeout) {
      ...
    } catch [other exceptions]
}

It's pretty textbook and everything works just fine; 这是一本漂亮的教科书,一切正常。 however during a recent network load test I discovered that my application was not receiving certain datagrams, and the problem worsened the more congested the network got. 但是,在最近的网络负载测试中,我发现我的应用程序未接收到某些数据报,并且随着网络的拥塞,问题变得更加严重。

Digging further I found that the timeout was triggering each time the the .receive() was called - almost as if there was nothing arriving on the port. 进一步挖掘,我发现每次调用.receive()时都会触发超时-几乎好像端口上没有任何东西。 However running Wireshark on the same machine shows that this is not true , and well-formed packets of expected size and content are arriving as usual. 但是,在同一台计算机上运行Wireshark表示这不是真的 ,并且正常大小的预期内容和格式的数据包将到达。 Somehow the datagrams just weren't finding their way to the application layer. 数据报以某种方式只是没有找到通往应用程序层的方式。

Ramping down the network traffic immediately alleviates this issue, and the Java application is immediately able to receive and process packets as usual. 降低网络流量可立即缓解此问题,并且Java应用程序能够立即照常接收和处理数据包。

Can anyone shed some light into what could possibly be the issue here, or what I can do further to troubleshoot this problem? 任何人都可以弄清楚这里可能是什么问题,或者我可以进一步解决该问题?

Thanks so much. 非常感谢。

UDP flood protection on a software firewall perhaps? 软件防火墙上的UDP泛洪保护?

Also, UDP is an unreliable protocol by design. 同样,UDP在设计上是不可靠的协议。 It could be that the network layer is discarding packets due to load. 网络层可能由于负载而正在丢弃数据包。

如果数据包到达主机,如果套接字发送缓冲区已满,主机仍可以将其丢弃,这是在应用程序无法跟上输入的情况下发生的。

I don't often program in Java, so maybe I'm missing something here, but why are you calling new DatagramSocket inside the loop? 我不经常用Java编程,所以也许我在这里缺少一些东西,但是为什么要在循环中调用new DatagramSocket It seems like that would create a new socket for every datagram, which would quickly exhaust system resources. 似乎这将为每个数据报创建一个新的套接字,这将很快耗尽系统资源。

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

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