简体   繁体   English

Java UDP套接字间歇接收

[英]Java UDP Socket receiving intermittently

1) I have a program that sends data via UDP from one server to another. 1)我有一个程序可以通过UDP将数据从一台服务器发送到另一台服务器。

2) Another program receives these, and simply forwards them via UDP to several destinations, via UDP. 2)另一个程序接收这些消息,然后简单地通过UDP将它们通过UDP转发到多个目的地。

3) One of the destinations is localhost. 3)目的地之一是localhost。

Data is being received in 2) and being sent to 3) in a constant flow with no break. 在2)中接收数据,并以不间断的恒定流将其发送到3)。 Yet the application receiving packets in 3) from 2) is receiving intermittently. 但是在2)中3)中接收数据包的应用程序正在间歇接收。 It will receive data for a 20 seconds, then not receive for a few minutes, and keeps on in a seemingly random fashion. 它会在20秒钟内接收数据,然后在几分钟内不接收数据,并以看似随机的方式继续运行。

This SAME code works perfect when receiving the packets externally. 当从外部接收数据包时,此SAME代码非常有效。 Also, if i run a tcpdump -i lo on the port that 3) is listening on, there is in fact a constant flow of data arriving to the port. 另外,如果我在3)正在侦听的端口上运行tcpdump -i lo,则实际上有恒定的数据流到达该端口。

I cannot understand where the problem could be? 我不明白问题可能在哪里?

-Steve -史蒂夫

When you are sending data in TCP you don't have to worry about flow control because TCP does it for you - you can send data as quickly as you like and the write method will block if you are sending it too fast. 当您使用TCP发送数据时,您不必担心流控制,因为TCP可以为您完成控制-您可以根据需要尽快发送数据,如果发送速度太快,write方法将被阻止。

This isn't the case in UDP, your packet write will be done and return immediately but if you are sending too many packets too quickly it will simply be discarded. 在UDP中不是这种情况,您的数据包写入将完成并立即返回,但是如果您发送太多的数据包太快,它将被简单地丢弃。 This could happen anywhere along the line from your OS, through all the routers and network devices through to the other machine. 这可能发生在从操作系统到所有路由器和网络设备再到另一台机器的任何地方。

You therefore need to have some scheme which limits the flow of packets yourself. 因此,您需要制定一些方案来自己限制数据包的流量。 To test if this is the problem just pop a sleep in between each send. 要测试这是否是问题,只需在每个发送之间插入一个睡眠。 If you find that sending 1 packet per second or 10 per second, all of them get through then when you remove the sleep you go back to 20 seconds OK and nothing for a few minutes you know its because your flow control is insufficient. 如果您发现每秒发送1个数据包或每秒发送10个数据包,它们全部都通过了,那么当您删除睡眠时,您可以返回20秒OK,并且几分钟后您什么都不知道,因为您的流量控制不足。

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

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