Sub topic 4
The TCP Segment: Header Fields, Sequence and Acknowledgement Numbers
This chapter assumes you know the four-layer model and encapsulation, and that you have seen the socket calls from the previous chapter. It covers the TCP header, how sequence and acknowledgement numbers work, and the calculations GATE asks about. Connection setup, retransmission, and window control get their own chapters, so they appear here only as far as the header fields require.
What a segment is
TCP gives the application a byte stream. The application hands over bytes in whatever chunks it likes, and TCP cuts the stream into pieces called segments. Each segment is carried inside one IP packet.
A segment has a header and a payload. The header is at least 20 bytes. Options can extend it to at most 60 bytes. The payload is the slice of the byte stream that the segment carries.
The Maximum Segment Size (MSS) is the largest payload TCP will put in one segment. It excludes the TCP and IP headers. With a typical Ethernet MTU of 1500 bytes, 20 bytes of IP header and 20 bytes of TCP header leave an MSS of 1460 bytes.
The header layout
| Field | Size | Purpose |
|---|---|---|
| Source port | 16 bits | Port of the sending process |
| Destination port | 16 bits | Port of the receiving process |
| Sequence number | 32 bits | Stream position of the first payload byte |
| Acknowledgement number | 32 bits | Next byte the sender of this segment expects to receive |
| Data offset (header length) | 4 bits | Header length in 32-bit words |
| Flags | 6 classic bits (URG, ACK, PSH, RST, SYN, FIN) | Control bits |
| Window size | 16 bits | Receiver window advertised to the peer |
| Checksum | 16 bits | Error detection over header, payload, and a pseudo-header |
| Urgent pointer | 16 bits | Used only when URG is set |
| Options | 0 to 40 bytes | MSS, window scale, timestamps, SACK |
Two facts are tested often:
- The data offset counts 32-bit words. The field holds 5 to 15. A value of 5 means 20 bytes and 15 means 60 bytes. Header length in bytes is the field value times 4.
- The TCP header has no length field for the payload. The payload length comes from the IP total length minus the IP header length minus the TCP header length.
Ports and the two IP addresses identify a connection. Together they form the 4-tuple (source IP, source port, destination IP, destination port).
The flags
| Flag | Meaning |
|---|---|
| SYN | Synchronize sequence numbers. Used to open a connection. |
| ACK | The acknowledgement number field is valid. Set on almost every segment after the first SYN. |
| FIN | The sender has no more data. Used to close its direction. |
| RST | Reset the connection, for example when a segment arrives for a port with no listener. |
| PSH | Deliver the data to the application now instead of buffering it. |
| URG | The urgent pointer field is valid. |
The checksum
The checksum covers the TCP header, the payload, and a pseudo-header taken from the IP layer: source IP, destination IP, protocol number, and TCP length. Including the IP addresses lets the receiver detect a segment delivered to the wrong host. It is the same 16-bit one's complement sum used by UDP, but in TCP it is mandatory.
Sequence numbers count bytes
This is the point most students get wrong. TCP numbers bytes, not segments.
- The sequence number in a segment is the stream number of the first payload byte in that segment.
- If a segment carries N bytes starting at sequence number S, it covers bytes S to S + N − 1.
- The next segment from that sender normally starts at S + N.
Each direction of a connection has its own sequence space. Each side picks its own initial sequence number (ISN), which is not necessarily zero. Random ISNs make it unlikely that a stray segment from an old connection is accepted by a new one.
The sequence number field is 32 bits, so the space is 2^32 values, and numbers wrap around to 0 after 2^32 − 1. At 1 Gbps, a full wrap takes about 2^32 × 8 / 10^9 ≈ 34 seconds. This is one reason very fast links need the timestamp option, which protects against old duplicate segments.
SYN and FIN each consume one sequence number even though they carry no payload. A pure ACK with no data consumes none.
Acknowledgement numbers are cumulative and name the next byte
The acknowledgement number is the sequence number of the next byte the receiver expects. It says: everything before this number has arrived in order.
- If the receiver has bytes up to 999 in order, it sends ACK = 1000.
- It does not send 999. This off-by-one is the most common error in trace questions.
- ACKs are cumulative. ACK = 1000 confirms bytes 0 to 999 together, so a lost ACK is often covered by a later one.
- If a segment arrives out of order, leaving a gap, the receiver keeps repeating the ACK for the first missing byte. These are duplicate ACKs, which the reliability chapter uses to trigger fast retransmit.
A segment can carry data and an acknowledgement at the same time. This is called piggybacking. The sequence number refers to the sender's own outgoing stream, and the acknowledgement number refers to the peer's stream. They live in separate number spaces.
A worked trace
Host A sends 1000 bytes of data to host B. A's ISN is 5000 (assume the connection is already open, and 5000 is the sequence number of the first data byte). A splits the data into segments of 400, 400, and 200 bytes. B's next byte to send is numbered 9000.
| Step | Sender | Seq | Payload | ACK field | Meaning |
|---|---|---|---|---|---|
| 1 | A | 5000 | 400 | 9000 | Covers bytes 5000–5399 |
| 2 | A | 5400 | 400 | 9000 | Covers bytes 5400–5799 |
| 3 | A | 5800 | 200 | 9000 | Covers bytes 5800–5999 |
| 4 | B | 9000 | 0 | 6000 | Cumulative ACK for all 1000 bytes |
If segment 2 is lost, B receives segment 1 and then segment 3. B replies ACK = 5400 to segment 1. When segment 3 arrives out of order, B sends ACK = 5400 again, a duplicate. The sequence numbers of segments 1 and 3 do not change, so A can see the gap.
Seeing the header in code
This Python program unpacks the fixed 20-byte header of a TCP segment. It builds a sample segment so that you can run it without a network.
import struct
def parse_tcp_header(segment: bytes) -> dict:
(src, dst, seq, ack, off_flags, window,
checksum, urgent) = struct.unpack("!HHIIHHHH", segment[:20])
header_len = (off_flags >> 12) * 4
flags = off_flags & 0x3F
names = ["FIN", "SYN", "RST", "PSH", "ACK", "URG"]
set_flags = [n for i, n in enumerate(names) if flags & (1 << i)]
return {
"src_port": src,
"dst_port": dst,
"seq": seq,
"ack": ack,
"header_len": header_len,
"flags": set_flags,
"window": window,
"payload_len": len(segment) - header_len,
}
# Data offset 5 (20 bytes), flags ACK + PSH, 400-byte payload.
off_flags = (5 << 12) | 0b011000
header = struct.pack("!HHIIHHHH", 49152, 80, 5000, 9000,
off_flags, 65535, 0, 0)
segment = header + b"x" * 400
print(parse_tcp_header(segment))The ! in the format string selects network byte order (big-endian). Flag bit positions in the low six bits are FIN = 1, SYN = 2, RST = 4, PSH = 8, ACK = 16, URG = 32. The program prints ACK and PSH as set, a header length of 20, and a payload length of 400.
Common errors
- Counting segments instead of bytes. After a segment with sequence number 5000 and 400 bytes, the next sequence number is 5400, not 5001.
- ACK = last byte received. The ACK is the last in-order byte plus one.
- Forgetting that SYN and FIN consume a sequence number. A SYN with sequence number x is acknowledged with x + 1, even though it has no data.
- Treating the data offset as bytes. It is in 4-byte words.
- Mixing the two directions. Each side has its own sequence space, so A's sequence number is never compared with B's.
- Ignoring wraparound. Sequence arithmetic is modulo 2^32.
Header overhead
Efficiency questions combine the header size with the payload. With a 20-byte TCP header, a 20-byte IP header, and a 1460-byte payload, the fraction of bytes that is useful data is 1460 / 1500 ≈ 97.3 percent. For a 1-byte payload, as in an interactive typing session, it is 1 / 41 ≈ 2.4 percent. This is why the Nagle algorithm and delayed ACKs exist.
Summary table for quick recall
| Quantity | Rule |
|---|---|
| Header length | Data offset × 4 bytes, between 20 and 60 |
| Payload length | IP total length − IP header length − TCP header length |
| Next sequence number | Seq + payload bytes (+1 if SYN or FIN is set) |
| ACK value | Next expected byte, equal to last in-order byte + 1 |
| Sequence space | 2^32, wraps modulo 2^32 |
| MSS | MTU − IP header − TCP header, typically 1460 |
Exercise
A TCP segment has sequence number 7200 and a data offset field of 8. The IP packet carrying it has a total length of 1100 bytes and an IP header length of 20 bytes. (a) How many payload bytes does the segment carry? (b) What acknowledgement number will the receiver send if this segment is the next one in order and arrives intact?
Show answer
(a) The TCP header length is 8 × 4 = 32 bytes. Payload = 1100 − 20 − 32 = 1048 bytes. (b) The segment covers bytes 7200 to 7200 + 1048 − 1 = 8247. The receiver expects byte 8248 next, so it sends ACK = 8248.
Exercise
Host A receives segments covering bytes 0–999 and 2000–2999 from host B, but the segment covering bytes 1000–1999 is lost. What acknowledgement number does A send after each arrival, and what is the name for the repeated one?
Show answer
After bytes 0–999 arrive, A sends ACK = 1000. After bytes 2000–2999 arrive out of order, A still has a gap starting at byte 1000, so it sends ACK = 1000 again. The repeated ACK is a duplicate ACK. Cumulative acknowledgements cannot acknowledge bytes past a gap.
Exercise
A host sends a SYN with initial sequence number 1000, then a data segment of 300 bytes immediately after the connection is established. What is the sequence number of the data segment, and what ACK value does the peer send for the SYN itself?
Show answer
The SYN consumes one sequence number, so the first data byte has sequence number 1001. The peer acknowledges the SYN with ACK = 1001, meaning it expects byte 1001 next. After the 300-byte segment, the peer would acknowledge 1301.