theAnonymous
theAnonymous

Reputation: 1804

DatagramPacket - does Java frag and rearrange it first?

I'm new to the whole UDP thing ('cause everyone loves TCP) and need to ask a few questions about Java's implementation.

I need somebody to tell me whether:

  1. The DatagramPackets sent by Java are fragmented automatically due to network configurations and data size.
  2. The DatagramPackets are rearranged to be in the correct frag sequential order by Java after being fragmented automatically due to network configurations and data size... before the receive() call returns the result.
  3. If fragmented DatagramPackets that're incomplete are dropped or generate Exceptions when dropped. (Some fragments received, others lost)

I'm concerned that Java drops it silently, or data is not arranged correctly... which would mean that I have to implement a pseudo TCP kind of thing to have both the benefits of UDP, as well as the checking of TCP.

Upvotes: 1

Views: 183

Answers (2)

user207421
user207421

Reputation: 310893

The DatagramPackets sent by Java are fragmented automatically due to network configurations and data size.

Yes, but Java has nothing to do with it.

The DatagramPackets are rearranged to be in the correct frag sequential order by Java after being fragmented automatically due to network configurations and data size... before the receive() call returns the result.

Yes, but not by Java, and only if all the fragments arrive. This happens at the IP layer.

If fragmented DatagramPackets that're incomplete are dropped or generate Exceptions when dropped. (Some fragments received, others lost)

They are dropped silently. No exception. Again Java has nothing to do with it. It all happens at the IP layer. If and only if all the fragments arrive, the datagram is reassembled and passed up to the UDP layer. Java is not involved in any way.

I'm concerned that Java drops it silently

Java does nothing. IP drops it silently.

or data is not arranged correctly

A datagram is received either intact and entire or not at all. Again Java has nothing to do with it.

which would mean that I have to implement a pseudo TCP kind of thing to have both the benefits of UDP, as well as the checking of TCP.

Correct. You do.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533500

UDP is largely implemented in the OS and Java has very little say in the matter.

  • packets over 576 bytes long can be fragmented;
  • packets can be lost;
  • packets can arrive out of order.

There is no way for Java, or you to tell whether these have happened.

What you can do is implement a protocol to detect this. e.g. adding a sequence number, length and checksum to the start of each packet.

which would mean that I have to implement a pseudo TCP kind of thing to have both the benefits of UDP, as well as the checking of TCP.

And now you are starting to understand why "everyone loves TCP" or most people do. UDP has its uses but for most applications TCP is simplest.

Upvotes: 2

Related Questions