3

When receiving TCP packets, Socket will give me reassembled packets, in case they got IP fragmented, as I'm guaranteed to get an ordered, gap-free stream of bytes.

When receiving UDP packets, where I may receive packets in a different order than sent, or duplicates, and other packets might get lost along the way, I would have expected to get every UDP/IP packet "raw", immediately when it arrives. On the other hand, as I don't get the IP header, I could not reassemble packets myself. In other words, I have to rely on Socket to do the job for me. But that would mean, that for this reassembling alone, (fragmented) packets would have to be buffered, ordered, de-duped, and discarded if a fragment doesn't make it.

How does this work in reality?

Evgeniy Berezovsky
  • 18,571
  • 13
  • 82
  • 156
  • Normally, for UDP, you would add a kind of header to each packet yourself, so you can identify the data and know when something got lost. You would also choose the packet size small enough to prevent splitting. If fragmentation _does_ happen, I don't know how to handle it. – PMF Jul 02 '14 at 04:56

1 Answers1

3

Reassembly occurs at the IP layer, and is transparent to you. In short you don't need to be concerned about it, other than for performance reasons, unless you are splitting up the packets yourself.

Ananke
  • 1,250
  • 9
  • 11
  • In other words, for reassembly, the IP layer already provides features similar to some of the higher layer TCP features: Ordering (and hence buffering) of packets, and a timeout when the unfinished group of fragments is discarded. Do you have any reference where I can verify your answer? – Evgeniy Berezovsky Jul 02 '14 at 23:22
  • 2
    That would be RFC 791, [starting on the bottom of page 24](http://tools.ietf.org/html/rfc791#page-24) – Scott Chamberlain Jul 03 '14 at 03:08
  • UDP does not guarantee order of packets, but will guarantee that datagrams will be complete if they are received...regardless of whether they are fragmented during transmission. A consequence of that is that if a packet is fragmented, and one part of that is lost or not received within the time window, you won't receive any of it. It is therefore best to generally keep to smaller datagram sizes. – Ananke Jul 03 '14 at 13:30
  • While reassembly does occur at the IP layer it is the IP layer of the final destination. Not the router or switch before the destination. `When an IP datagram is fragmented, it is not reassembled until it reaches its final destination. ` [This link](http://www.pcvr.nl/tcpip/udp_user.htm) explains it very well, and I suggest [WireShark](https://wiki.wireshark.org/IP_Reassembly) for looking at this and verifying. – Nelda.techspiress Aug 25 '17 at 16:28