The Transport Layer

a sequence number

If you mail someone a long letter in many envelopes, you'd number the pages — 'page 1 of 40', 'page 2 of 40' — so even if the envelopes arrive jumbled or one gets lost, the reader can reassemble the letter in order and notice what's missing. A sequence number is that page number for network data. It labels data so the receiver can order it correctly, detect duplicates, and discover gaps.

TCP numbers the byte stream, not the segments: the sequence number in a segment's header is the position, in the overall stream, of that segment's first byte. So if a segment's sequence number is 4001 and it carries 500 bytes, those are stream bytes 4001 through 4500, and the next segment should start at 4501. The receiver uses these numbers to lay bytes down in the right order in its buffer even if segments arrive out of order, and to throw away a duplicate (a retransmission that arrived twice). Acknowledgment numbers are the mirror image — they refer to the same stream positions.

Why it matters: sequence numbers are the backbone of in-order, duplicate-free delivery; without them the byte stream couldn't be reassembled. One important detail: TCP does not start counting at 1. Each side chooses a fresh, hard-to-guess initial sequence number during the handshake, which both helps distinguish bytes of a new connection from stale bytes of an old one and makes it harder for an attacker to inject forged data. The counter is 32 bits and wraps around on long, fast transfers, which TCP handles carefully.

After a handshake fixes the initial sequence number at, say, 9000, the first 1460-byte segment carries sequence number 9001 (bytes 9001–10460); the next is 10461. If 10461 arrives before 9001 due to reordering, the receiver still places each correctly using these numbers.

Sequence numbers count bytes in the stream, not segments.

TCP sequence numbers count bytes, not packets, and don't start at zero — each connection begins at a randomized initial value. That randomness is a (modest) defense against off-path attackers guessing the sequence to inject data.

Also called
seq number序號序列號碼