非阻塞UDP I / O与阻止Java中的UDP I / O. [英] Non-blocking UDP I/O vs blocking UDP I/O in Java

查看:172
本文介绍了非阻塞UDP I / O与阻止Java中的UDP I / O.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.)

即使 DatagramChannel <,UDP发送操作也似乎无法阻止/ code>未在阻止模式下运行。是否存在 DatagramSocket.send(DatagramPacket)由于拥塞或类似情况而阻塞的情况?我真的很好奇是否存在这样的情况以及生产环境中可能出现的情况。

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.

如果 DatagramSocket.send(DatagramPacket)实际上没有阻塞,我不打算使用连接的 DatagramSocket 并只绑定到一个端口,使用非阻塞没有优势模式与 DatagramChannel 选择器

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?

推荐答案

我已经使用了Java的DatagramSockets,Channels等,但我仍然可以给你一些帮助。

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

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

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.

这篇关于非阻塞UDP I / O与阻止Java中的UDP I / O.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆