Java 中非阻塞 UDP I/O 与阻塞 UDP I/O

发布于 2024-07-13 18:30:28 字数 504 浏览 7 评论 0原文

NIO 中的非阻塞 TCP/IP SocketChannelSelector 帮助我用少量线程处理许多 TCP/IP 连接。 但是 UDP DatagramChannels 怎么样? (我必须承认我对 UDP 不是很熟悉。)

即使 DatagramChannel 未在阻塞模式下运行,UDP 发送操作似乎也不会阻塞。 是否真的存在 DatagramSocket.send(DatagramPacket) 由于拥塞或类似原因而阻塞的情况? 我真的很好奇是否存在这种情况以及生产环境中存在哪些可能的情况。

如果 DatagramSocket.send(DatagramPacket) 实际上没有阻塞,并且我不打算使用连接的 DatagramSocket 并仅绑定到一个端口,那么使用非- 使用DatagramChannelSelector 进行阻塞模式?

Non-blocking TCP/IP SocketChannels and Selector in NIO help me to handle many TCP/IP connections with small number of threads. But how about UDP DatagramChannels? (I must admit that I'm not very familiar with UDP.)

UDP send operations don't seem to block even if the DatagramChannel is not operating in blocking mode. Is there really a case where DatagramSocket.send(DatagramPacket) blocks due to congestion or something similar? I'm really curious if there's such a case and what the possible cases exist in a production environment.

If DatagramSocket.send(DatagramPacket) doesn't actually block and I am not going to use a connected DatagramSocket and bind to only one port, is there no advantage of using non-blocking mode with DatagramChannel and Selector?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安静被遗忘 2024-07-20 18:30:28

我已经有一段时间没有使用Java的DatagramSockets、Channels之类的东西了,但我仍然可以给你一些帮助。

UDP 协议不像 TCP 那样建立连接。 相反,它只是发送数据并忘记它。 如果确保数据实际到达那里很重要,那么这是客户的责任。 因此,即使您处于阻塞模式,您的发送操作也只会在刷新缓冲区时阻塞。 由于 UDP 对网络一无所知,因此它会尽早将其写出,而不检查网络速度或是否确实到达了它应该去的地方。 因此,对您来说,该通道似乎实际上已立即准备好进行更多发送。

It's been a while since I've used Java's DatagramSockets, Channels and the like, but I can still give you some help.

The UDP protocol does not establish a connection like TCP does. Rather, it just sends the data and forgets about it. If it is important to make sure that the data actually gets there, that is the client's responsibility. Thus, even if you are in blocking mode, your send operation will only block for as long as it takes to flush the buffer out. Since UDP does not know anything about the network, it will write it out at the earliest opportunity without checking the network speed or if it actually gets to where it is supposed to be going. Thus, to you, it appears as if the channel is actually immediately ready for more sending.

薯片软お妹 2024-07-20 18:30:28

UDP 不阻塞(仅在将数据传输到操作系统时阻塞)
这意味着如果在任何时候下一跳/交换机/机器无法缓冲 UDP 数据包,它就会丢弃该数据包。 在某些情况下,这可能是理想的行为。 但这是您需要注意的事情。

UDP 也不保证

  • 按数据包发送的顺序传送数据包。
  • 不要分解大数据包。
  • 跨交换机转发数据包。 通常交换机之间的 UDP 转发是关闭的。

然而,UDP 确实支持多播,因此可以将相同的数据包传送到一台或多台主机。 然而,发送者不知道是否有人收到数据包。

UDP 的一个棘手问题是它在大多数情况下都能工作,但有时会以难以重现的方式严重失败。 因此,即使您做了一些测试并且看起来有效,您也不应该假设可靠性。

UDP doesn't block (It only blocks while it is transferring the data to the OS)
This means if at any point the next hop/switch/machine cannot buffer the UDP packet it drops it. This can be desirable behaviour in some situations. But it is something you need to be aware of.

UDP also doesn't guarantee to

  • delivery packets in the order they are sent.
  • not to break up large packets.
  • forward packets across switches. Often UDP forwarding between switches is turned off.

However UDP does support multicast so the same packet can be delivered to one or more hosts. The sender has no idea if anyone receives the packets however.

A tricky thing about UDP is it works most of the time, but fails badly sometimes in ways which are very difficult to reproduce. For this reason, you shouldn't assume reliability even if you do a few tests and it appears to work.

你爱我像她 2024-07-20 18:30:28

非阻塞 UDP 在接收端最有用。
数据包发送只能因本地情况而延迟:诸如“游戏网卡”之类的本地流量整形工具(将游戏流量优先于其他流量源)或过载的网卡(这种情况不太可能发生)可能会延迟数据包的发送。 一旦脱离系统。 一旦数据包离开本地接口,应用程序就不再关心它了。

Non blocking UDP is mostly useful on the receiving side.
Packet sending can only be delayed due to local circumstances: local traffic shaping tools like "gaming network cards" that prioritize gaming traffic over other traffic sources, or overloaded network card (which is not likely to happen) can delay the sending of a packet. Once out of the system. Once the packet leaves the local interface, it's no longer the application's concern.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文