java 为什么即使进程在本地运行,UDP 数据报也会乱序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2533873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do I get UDP datagrams out of order even with processes running locally?
提问by Bilthon
I'm developing a java interface between a streaming server and a flash client. I noticed that UDP datagrams can reach my interface out of order even if both processes are running locally.
我正在开发流服务器和 Flash 客户端之间的 Java 接口。我注意到即使两个进程都在本地运行,UDP 数据报也可以无序地到达我的接口。
Is that normal? I thought that as no datagram has to go through any router or any network device, then that should not be happening.
这是正常的吗?我认为由于没有数据报必须通过任何路由器或任何网络设备,因此不应该发生这种情况。
采纳答案by Hyman
Actually there are no guarantees of ordering and reception about UDP packets, even if they are sent by localhost on localhost. Just because the specification of the protocol doesn't imply anything about it.
实际上,无法保证 UDP 数据包的排序和接收,即使它们是由 localhost 在 localhost 上发送的。仅仅因为协议的规范并不暗示任何关于它的内容。
Since you can't make assumptions on them you should choose to use TCP or handle reordering by using a sequence number handled by your programs..
由于您无法对它们做出假设,因此您应该选择使用 TCP 或使用程序处理的序列号来处理重新排序。
回答by Thomas M. DuBuisson
This would be operating system dependent. While you failed to specify an operating system it isn't important anyway. To remain portable you should always anticipate your datagram sockets receiving out of order data.
这将取决于操作系统。虽然您未能指定操作系统,但这并不重要。为了保持可移植性,您应该始终预期您的数据报套接字接收无序数据。
回答by Hyman
Although you are running localhost, expect UDP datagrams to be out of sequence in actual deployment.
尽管您正在运行 localhost,但预计 UDP 数据报在实际部署中会出现乱序。
If you need them in sequence, try TCP.
如果按顺序需要它们,请尝试 TCP。
回答by user207421
UDP isn't specified to preserve sequence, as the posters above have all said, but if there are no intermediate routers I would also suspect a bug in your code.
UDP 没有被指定为保留序列,正如上面的海报所说的那样,但如果没有中间路由器,我也会怀疑您的代码中存在错误。

