Protocol Tutorial

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)

Header Size: 20 bytes (160 bits)
Fields: 10
Endianness: big
Spec: 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.

1

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 Example
2

SYN-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 Example
3

ACK - Acknowledge

The client sends an ACK packet back to confirm the connection. Only the ACK flag is set, completing the handshake.

View TCP ACK Example

TCP Flags Explained

SYN

Synchronize sequence numbers to initiate connection

ACK

Acknowledge received data

FIN

Finish - gracefully terminate connection

RST

Reset - abort connection immediately

Learn more about TCP Flags

Header Fields

FieldOffsetSizeTypeDescription
Source Port
source_port
0 bits16 bitsuint16Source port number
Known values: 20=FTP Data, 21=FTP Control, 22=SSH, 23=Telnet, +10 more
Destination Port
dest_port
16 bits16 bitsuint16Destination port number
Known values: 20=FTP Data, 21=FTP Control, 22=SSH, 23=Telnet, +10 more
Sequence Number
sequence
32 bits32 bitsuint32Sequence number of the first data byte in this segment
Acknowledgment Number
acknowledgment
64 bits32 bitsuint32Next sequence number the sender expects to receive
Data Offset
data_offset
96 bits4 bitsuint8Size of TCP header in 32-bit words (minimum 5, maximum 15)
Reserved
reserved
100 bits3 bitsuint8binaryReserved for future use (must be zero)
Flags
flags
103 bits9 bitsuint16binaryControl flags (NS, CWR, ECE, URG, ACK, PSH, RST, SYN, FIN)
Window Size
window
112 bits16 bitsuint16Size of the receive window (flow control)
Checksum
checksum
128 bits16 bitsuint16hexChecksum for error detection
Urgent Pointer
urgent
144 bits16 bitsuint16Offset 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