Non-blocking UDP I/O vs blocking UDP I/O in Java

后端 未结 3 2018
一整个雨季
一整个雨季 2021-02-04 04:39

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

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-04 04:47

    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.

提交回复
热议问题