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

后端 未结 3 2017
一整个雨季
一整个雨季 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 05:04

    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.

提交回复
热议问题