Sub topic 6
TCP segments and the TCP header
This chapter assumes you know the layered model, what an IP packet is, and what a port is. It covers how TCP packages a byte stream into segments and what the header fields do. The handshake, retransmission and windows get their own chapters. Here you only learn where each mechanism lives in the header.
From byte stream to segments
TCP presents a continuous stream of bytes to the application. Underneath, it cuts that stream into chunks called segments. Each segment is a TCP header followed by a piece of the data (the payload). TCP hands each segment to IP, which wraps it in a packet.
The size of the payload is limited by the maximum segment size (MSS). A typical Ethernet link carries 1500 bytes per IP packet. Subtract 20 bytes for the IPv4 header and 20 bytes for the basic TCP header, and the MSS is usually 1460 bytes. The two ends announce their MSS during the handshake.
The header fields
The basic header is 20 bytes. Options can extend it up to 60 bytes.
| Field | Size | Purpose |
|---|---|---|
| Source port | 16 bits | Port of the sending socket |
| Destination port | 16 bits | Port of the receiving socket |
| Sequence number | 32 bits | Position in the byte stream of the first payload byte |
| Acknowledgment number | 32 bits | Next byte the sender expects to receive |
| Data offset | 4 bits | Header length in 32-bit words |
| Flags | 9 bits | Control bits such as SYN, ACK, FIN, RST |
| Window size | 16 bits | How many bytes the sender of this segment can accept |
| Checksum | 16 bits | Detects corruption of header and data |
| Urgent pointer | 16 bits | Marks urgent data (rarely used) |
| Options | 0 to 40 bytes | MSS, window scaling, timestamps, selective acknowledgments |
Compare this with UDP, whose header is 8 bytes. The extra fields are the cost of reliability, ordering and flow control.
Ports
The two port fields, combined with the IP addresses in the packet, identify one connection. A server on port 443 can hold thousands of connections at once because each client has a different address and source port.
Sequence and acknowledgment numbers
TCP numbers bytes, not segments. The sequence number is the stream position of the segment's first payload byte. If a segment has sequence number 1000 and carries 500 bytes, the next segment starts at 1500. The acknowledgment number tells the peer "I have received everything before this byte; send this one next". Acknowledgments are cumulative.
Data offset
The header length varies because of options, so the receiver needs to know where the payload starts. Data offset counts 32-bit words. The minimum value is 5 (20 bytes) and the maximum is 15 (60 bytes).
Flags
Each flag is one bit. The most common ones:
- SYN opens a connection and synchronizes sequence numbers.
- ACK says the acknowledgment number is valid. It is set on nearly every segment after the first.
- FIN says the sender has no more data.
- RST aborts the connection immediately.
- PSH asks the receiver to pass data to the application without waiting.
Window size
The window field advertises how much more data the sender of this segment is willing to buffer. This is flow control, covered in a later chapter. Because 16 bits caps the value at 65,535, a window scale option multiplies it for fast, long links.
Checksum
The checksum covers the header, the payload, and a pseudo-header with the IP addresses. A receiver that finds a mismatch discards the segment, and the sender's retransmission logic recovers it.
Looking at a real header
You can build a header by hand to see the layout. This Python program packs a SYN segment with no options and unpacks it again.
import struct
src_port, dst_port = 54321, 80
seq, ack = 1000, 0
data_offset = 5 # 5 words * 4 bytes = 20 bytes
flags = 0x02 # SYN
window = 65535
checksum, urgent = 0, 0 # checksum left as 0 in this demo
offset_flags = (data_offset << 12) | flags
header = struct.pack("!HHIIHHHH", src_port, dst_port, seq, ack,
offset_flags, window, checksum, urgent)
print(len(header), "bytes")
s, d, sq, ak, of, win, ck, ug = struct.unpack("!HHIIHHHH", header)
print("header length:", (of >> 12) * 4)
print("SYN set:", bool(of & 0x02))
print("ACK set:", bool(of & 0x10))The ! means network byte order (big-endian), which all header fields use. The output is 20 bytes, header length: 20, SYN set: True, ACK set: False.
Common mistakes
- Thinking sequence numbers count segments. They count bytes.
- Forgetting that the header can be longer than 20 bytes. Always use data offset to find the payload.
- Treating the window field as a fixed 64 KB limit. With window scaling it can be far larger.
Exercise
A segment has sequence number 4000, data offset 8, and the whole TCP segment is 532 bytes long. What is the payload length, and what sequence number will the next segment from this sender carry (assuming no gaps)?
Show answer
The header length is 8 x 4 = 32 bytes, so the payload is 532 - 32 = 500 bytes. The next segment's sequence number is 4000 + 500 = 4500.
Exercise
Why does TCP need a data offset field when UDP has no equivalent?
Show answer
TCP headers have variable-length options, so the receiver must be told where the header ends and the payload begins. The UDP header is always 8 bytes, so no such field is needed.