Sub topic 8
How TCP Delivers Data Reliably and in Order
This chapter assumes you know the layered network model, what an IP packet is, and how the three-way handshake opens a connection. IP is best-effort: packets can be lost, duplicated, delayed, or reordered. TCP builds a reliable, ordered byte stream on top of that using sequence numbers, acknowledgments, and retransmission.
Sequence numbers count bytes
TCP numbers every byte it sends, not every segment. Each segment header carries a sequence number, which is the number of the first data byte in that segment. The initial value is chosen during the handshake.
If a segment starts at sequence number 1000 and carries 500 bytes, it covers bytes 1000 to 1499. The next segment starts at 1500. The receiver uses these numbers to put data back in order and to discard duplicates.
Acknowledgments are cumulative
The acknowledgment number in a segment says: "I have received every byte before this number, and I expect this one next." This is a cumulative acknowledgment.
For the segment above, the receiver replies with acknowledgment number 1500. If it later receives bytes 2000 to 2499 but still lacks 1500 to 1999, it keeps sending acknowledgment 1500. These repeated acknowledgments are called duplicate ACKs, and they signal a gap.
Retransmission
The sender keeps each segment in a buffer until it is acknowledged. It resends a segment in two main cases:
- Timeout. The sender starts a retransmission timer, called the RTO (retransmission timeout). The sender derives the RTO from measured round-trip times. If no acknowledgment arrives before the timer expires, the sender resends the segment. It also increases the timeout after each failure.
- Fast retransmit. If the sender receives three duplicate ACKs for the same number, it assumes that segment was lost. It resends it immediately, without waiting for the timer.
Fast retransmit recovers faster because it does not wait for the full timeout.
After the resent segment arrives, the receiver already holds everything up to byte 3499. It sends one cumulative acknowledgment, 3500.
Delivering in order
The receiver keeps out-of-order data in a buffer. It hands bytes to the application only when there is no gap before them. The application therefore never sees bytes 2000 to 2499 before bytes 1500 to 1999. A lost segment delays all later data until it is repaired. This is called head-of-line blocking.
Checksums catch corruption
Each segment carries a checksum. A receiver that finds a bad checksum drops the segment silently. No acknowledgment is sent, so the sender's timer or duplicate ACKs trigger a retransmission. Corruption is thus handled by the same mechanism as loss.
Common errors and misunderstandings
- Believing a successful
sendmeans the peer received the data.sendallreturns when the data is copied into the local send buffer. Delivery is confirmed only by the protocol's acknowledgments, which the application does not see. If the application needs confirmation, it must send its own reply. - Assuming one write equals one read. TCP guarantees byte order, not message boundaries. The next chapter covers this.
- Confusing acknowledgment numbers with the last byte received. The acknowledgment number is the next byte expected, which is one past the last contiguous byte.
- Expecting reliability to mean bounded delay. TCP guarantees delivery or an error, not timing. Under loss, retransmission timeouts can add noticeable delay.
Exercise
A receiver has received bytes 0 to 999 and 2000 to 2999, but not 1000 to 1999. What acknowledgment number does it send, and what must happen before the application can read bytes 2000 to 2999?
Show answer
It sends acknowledgment number 1000, because cumulative acknowledgments name the next byte expected after the last contiguous byte. The sender must retransmit bytes 1000 to 1999. Once they arrive, the receiver acknowledges 3000 and delivers bytes 1000 to 2999 to the application in order.
Exercise
Why does TCP's fast retransmit trigger on three duplicate ACKs instead of one?
Show answer
Network reordering can make a single duplicate ACK appear even when no segment was lost. Waiting for three makes it likely that a segment is really missing, so the sender avoids unnecessary retransmissions.