TCP Header Format
Comprehensive guide to TCP header structure: source port, destination port, sequence number, acknowledgment number, data offset, reserved, flags, window size, checksum, urgent pointer, and options.
Protocol Overview
Transmission Control Protocol (RFC 793)
TCP Three-Way Handshake
TCP uses a three-way handshake to establish a reliable connection. Follow the steps below to understand how SYN, SYN-ACK, and ACK packets work together.
SYN - Synchronize
The client sends a SYN packet to the server to initiate connection. The SYN flag is set, and a random sequence number is generated.
View TCP SYN ExampleSYN-ACK - Synchronize-Acknowledge
The server responds with both SYN and ACK flags set. The acknowledgment number equals the client's sequence number + 1.
View TCP SYN-ACK ExampleACK - Acknowledge
The client sends an ACK packet back to confirm the connection. Only the ACK flag is set, completing the handshake.
View TCP ACK ExampleTCP Flags Explained
Synchronize sequence numbers to initiate connection
Acknowledge received data
Finish - gracefully terminate connection
Reset - abort connection immediately
Header Fields
| Field | Offset | Size | Type | Description |
|---|---|---|---|---|
Source Port source_port | 0 bits | 16 bits | uint16 | Source port number Known values: 20=FTP Data, 21=FTP Control, 22=SSH, 23=Telnet, +10 more |
Destination Port dest_port | 16 bits | 16 bits | uint16 | Destination port number Known values: 20=FTP Data, 21=FTP Control, 22=SSH, 23=Telnet, +10 more |
Sequence Number sequence | 32 bits | 32 bits | uint32 | Sequence number of the first data byte in this segment |
Acknowledgment Number acknowledgment | 64 bits | 32 bits | uint32 | Next sequence number the sender expects to receive |
Data Offset data_offset | 96 bits | 4 bits | uint8 | Size of TCP header in 32-bit words (minimum 5, maximum 15) |
Reserved reserved | 100 bits | 3 bits | uint8binary | Reserved for future use (must be zero) |
Flags flags | 103 bits | 9 bits | uint16binary | Control flags (NS, CWR, ECE, URG, ACK, PSH, RST, SYN, FIN) |
Window Size window | 112 bits | 16 bits | uint16 | Size of the receive window (flow control) |
Checksum checksum | 128 bits | 16 bits | uint16hex | Checksum for error detection |
Urgent Pointer urgent | 144 bits | 16 bits | uint16 | Offset from sequence number indicating last urgent data byte |
Common TCP Mistakes
- 1Confusing sequence and acknowledgment numbers: sequence is what you send, acknowledgment is what you expect to receive
- 2Forgetting that Data Offset is in 32-bit words, not bytes (multiply by 4 to get header length in bytes)
- 3Assuming all 9 flag bits are used in practice (NS, CWR, ECE are rarely used in basic implementations)
- 4Not accounting for TCP options which can extend the header beyond 20 bytes
- 5Misunderstanding that Window Size is the receiver's buffer, not the sender's