Using the NIO DatagramChannel, will I need to process partially read / written packages?

When using SocketChannel, you need to save read and write buffers to handle partial write and read.

I have a suspicious suspicion that this might not be necessary when using the DatagramChannel, but there is little information.

What story?

Do I have to repeatedly (non-block) ByteBuffer until I get zero to read all pending datagrams?

When sending to non-blocking mode, can I rely on send (ByteBuffer, SocketAddress) to either send the entire buffer, or completely reject it, or do I need to save partially written buffers?

+3
source share
1 answer

Each reading of a datagram is an entire datagram, nothing more, nothing more. There is a hint that this is the case in the java.nio.DatagramChannel.read description:

If in datagrams than to remain in these buffers, then the rest of the datagram is silently discarded

When you are dealing with a SocketChannel, this is a message flow; There is no guarantee how much or how much data you will receive on each reading, as TCP reassembles separate packets to recreate the message on the other hand. But for UDP (this is what you read from the DatagramChannel) each packet is its own atomic message.

+5
source

Source: https://habr.com/ru/post/1705755/


All Articles