Search code examples
javanetwork-programmingudpdatagram

java datagramchannel data loss


I've read some conflicting things about how UDP/Java datagram channels operate. I need to know a few things:

  • Does UDP have an inherit way to tell if the packet that is received whole, and in order, before .read(ByteBuffer b) is called? I've read in at least one article saying that UDP inherit'ly discards incomplete or out of order data.

  • Does datagramchannel treat one send(buffer.. ) as one datagram packet? what if its a partial send?

  • Can a .read(.. ) read more than one packet of data, resulting in data being discarded if the buffer being given as the commands argument was only designed to handle one packet of data?


Solution

  • Does UDP have an [inherent] way to tell if the packet that is received whole, and in order, before .read(ByteBuffer b) is called? I've read in at least one article saying that UDP inherit'ly discards incomplete or out of order data.

    Neither statement is correct. It would be more accurate to say that IP has a way to tell if a datagram's fragments have all arrived, and then and only then does it even present it to UDP. Reassembly is the responsibility of the IP layer, not UDP. If the fragments don't arrive, UDP never even sees it. If they expire before reassembly is complete, IP throws them away.

    Before/after read() is called is irrelevant.

    Does datagramchannel treat one send(buffer.. ) as one datagram packet?

    Yes.

    what if it's a partial send?

    There is no such thing in UDP.

    Can a read(.. ) read more than one packet of data

    A UDP read will return exactly and only one datagram, or fail.

    resulting in data being discarded if the buffer being given as the commands argument was only designed to handle one packet of data?

    Can't happen.

    Re your comment below, which is about a completely different question, the usual technique for detecting truncation is to use a buffer one larger than the largest expected datagram. Then if you ever get a datagram that size, (i) it's an application protocol error, and (ii) it may have been truncated too.