Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does TCP implement/guarantee in-order data transmission?

I was wondering how exactly does TCP implement in-order delivery.

lets say this is list of events

  1. packet1 sent , ack received.
  2. packet2 sent , ack not-received.
  3. packet3 sent.
  4. packet4 sent.
  5. ack4 received.
  6. ack3 received.
  7. ack2 received.

Can you describe to me what exactly happens sequentially?

like image 877
Dunes Buggy Avatar asked Mar 30 '12 03:03

Dunes Buggy


1 Answers

The short answer is that each packet contains offset information (disguised as sequence number), specifying where in the stream its payload lies.

Let's say the following occurred: packet 1 is received, packet 2 is not received, and packet 3 and 4 are received. At this point receiving TCP stack knows where to copy contents of packets 3 and 4 on the buffer, and it knows that it still hasn't received prior data, so it will make packet 1 data available for reading, but it won't make packet 3 or 4 data available until packet 2 is received.

Transmitting TCP stack generally does not wait for acknowledgements for any single packet before sending out the next one, but if it does not receive an acknowledgement for a given packet (and ACKs can and are bundled together in a single packet for efficiency), it will retransmit it until an ACK is received.

Exact sequence of events depends on network conditions, TCP stack implementation, chosen TCP policy, socket options and other factors.

like image 140
George Skoptsov Avatar answered Sep 20 '22 13:09

George Skoptsov