Astrid
Astrid

Reputation: 33

Dealing with UDP unreliability

I'm using DatagramPacket and DatagramSocket classes in Java to send messages with UDP. I have incomplete knowledge about networking. I know that:

  1. when a datagram is sent, it may in fact be split into multiple pieces of data travelling independently on the network (for example, if my datagram length is greater than MTU).

  2. UDP does not guarantee the order of messages at receiving (and does not guarantee the receiving of messages at all).

Putting this information together, I "understand" that if I send one (large) DatagramPacket, I may receive the bytes of my datagram in any order (and some parts may even be missing)! But I think I misunderstood something because if it was the case, nobody would use such a protocol.

How can I ensure that the datagram I receive (if I receive it) is equal to the datagram I have sent?

Upvotes: 0

Views: 298

Answers (2)

user207421
user207421

Reputation: 311055

Putting this information together, I "understand" that if I send one (large) DatagramPacket, I may receive the bytes of my datagram in any order

No.

(and some parts may even be missing)!

No.

You will receive a UDP datagram intact and entire or not at all.

But I think I misunderstood something because if it was the case, nobody would use such a protocol.

Correct. It isn't the case.

How can I ensure that the datagram I receive (if I receive it) is equal to the datagram I have sent?

It always is. If it arrives. However it may arrive zero, one, or more times, and it may arrive out of order.

The generally accepted maximum practical UDP datagram size is 534 bytes of payload. You are guaranteed that IP will not fragment that, either at the sender or at any intermediate host, and non-fragmentation decreases your chance of packet loss. (If any fragment is lost the datagram is lost, as stated by @ottomeister.)

If sequence is important to you, you need sequence numbers in your datagrams. This can also help to protect you against duplicates, as you know what sequence number you're up to so you can spot a duplicate.

If arrival is important to you, you need an ACK- or NACK-based application protocol.

Upvotes: 0

ottomeister
ottomeister

Reputation: 5828

Your understanding is incorrect. If your datagram is broken into fragments by IP (below the UDP layer) at the sending side, then IP at the receiver will reassemble those fragments in the correct order before passing the entire reassembled datagram up to the receiver's UDP layer. If any fragments of the datagram are lost then the reassembly will fail, the partially-reconstructed datagram will be discarded, and nothing will be passed up to the receiver's UDP layer. So the receiving UDP -- and therefore the receiving application -- gets either a complete datagram or nothing. It will never get a partial datagram, and it will never get a datagram whose content has been scrambled because of fragmentation.

The receiving application can be given a partial (truncated) datagram if the incoming datagram is larger than the application's receive buffer, but that has nothing to do with fragmentation.

Upvotes: 1

Related Questions