ConceptDifficulty: Beginner2 min

Sequence Numbers and Acknowledgements

Every byte has a number, and the receiver says which byte it wants next.

TCP gives every byte it sends a number, the sequence number, and the receiver replies with an acknowledgement number: the number of the next byte it expects. Because the acknowledgement always names the first missing byte, the sender can tell exactly what has arrived and what has not, which is the base of everything else TCP does.

Each segment's header carries the sequence number of its first byte. A segment with sequence number 1001 that carries 100 bytes holds bytes 1001 to 1100, so the next segment starts at 1101. When those 100 bytes arrive, the receiver acknowledges 1101: everything before it is here, and 1101 is what it wants next.

Three segments of 100 bytes and the acknowledgement after each

InputA stream starting at sequence number 1001, sent in segments of 100 bytes

  1. Segment 1 arrives

    1. seq 1001
    2. 100 bytes
    3. ACK 1101acknowledged

    Bytes 1001 to 1100 are here; the next byte wanted is 1101.

  2. Segment 2 arrives

    1. seq 1101
    2. 100 bytes
    3. ACK 1201acknowledged

    Bytes up to 1200 are here.

  3. Segment 3 arrives

    1. seq 1201
    2. 100 bytes
    3. ACK 1301acknowledged

    Bytes up to 1300 are here.

ResultThe acknowledgement is always the sequence number plus the bytes carried: the next byte expected.

Two properties follow from the design.

Acknowledgements are cumulative. An acknowledgement of 1301 says every byte before 1301 has arrived, not only the last segment. A lost acknowledgement therefore costs nothing, because the next one covers it.

Loss becomes visible. If segment 2 is lost and segment 3 arrives, the receiver still has nothing after byte 1100 that it can deliver in order, so it acknowledges 1101 again. A repeated acknowledgement tells the sender a gap exists, and where.

Sequence numbers do not start at zero. Each side picks a random starting number for each connection, the initial sequence number, so a stray segment from an old connection, or one forged by an attacker, is unlikely to fit. The examples here use small round numbers to keep the arithmetic easy to follow.

The number is 32 bits, so it wraps round after about 4 GB and starts again; a fast connection handles that with a timestamp option.

So when you read a packet capture, the acknowledgement number answers one question directly: up to which byte has the other side received everything.

← → move through sections and cards