WWW

How the Internet Works, Chapter 10
The Transport Layer: How TCP Guarantees Message Delivery

posted in: How the Internet Works | 0

TCP guarantees message delivery by implementing four requirements:

  • Delivering segments in the order sent
  • Detecting corrupt segments
  • Retransmission of lost or corrupt segments
  • Elimination of duplicate segments

A TCP segment uses four of its fields to implement these requirements:

    1. Sequence Number
    2. Acknowledgement Number
    3. SYN, ACK and FIN flags from the nine flag bits
    4. Payload (specifically, the Payload size)

SYN and ACK Numbers

The sequence number (SYN) is used to determine the order of the segments sent. The sequence number for a given segment is the number of the previously sent segment (the first sequence number is randomly generated) plus the size of the payload sent. (The size of the payload is variable, but can be calculated by subtracting the sizes of the other fields from the total size of the PDU.) As such, the sequence number minus the first sequence number is the total number of appication-layer bytes sent to the destination so far (including the size of the current payload). Segments sorted in sequence-number order are in the order that they were sent.

The acknowledgement number (ACK) is used to identify lost segments. The acknowledgement number is the acknowledgement number of the previously received segment plus the size of the payload being received. As such, it is the total number of transport-level bytes received so far (including the current segment).

Steps to Send Data

The process of sending a data transmission has these steps:

    1. Once handshaking is complete, the server starts sending segments. (Most transmissions won’t fit in a single segment, so they are broken up into multiple ones.)
    2. As a client receives segments, it responds by sending segments with the ACK flag set (typically about one such response for every two data segments sent), and the acknowledgement number set to the number of the next expected sequence number.
    3. If the server doesn’t get an acknowledgement number corresponding to the sequence number of the initial packet within a specified time, it assumes the packet is lost and resends it.
    4. If the server receives two ACK segments with the same acknowledgement number, it resends segments starting with the one with the sequence number corresponding to the duplicate acknowledgement number. (In effect, the client is saying “hey, I’m still expecting segment x,” and the server is saying “I already sent it, but I’ll send it again.”)
    5. If a checksum fails, TCP drops the segment, and keeps the sequence and acknowledgement numbers as they are. This means that the sender resends corrupt segments just as it does lost ones.
    6. Sometimes a packet arrives at the client destination, but the ACK segment containing the acknowledgement of same doesn’t make it back to the server. In this case, the server treats the packet as lost and resends it. The client will then receive a duplicate. In this case, the client drops the duplicate and resends the appropriate ACK segment.
    7. The sender sets the FIN flag in the final segment of data sent. The receiver then sends an ACK segment as usual, and then another with a FIN flag of its own. The sender then sends another ACK segment and the connection is closed.

These steps, then, implement the four requirements we mentioned up front. The next article covers steps to improve transmission performance.