TCP Header Structure Explained: Ports, Sequence Numbers, Flags, Window and Options

Cyber Security3 min read
TCP header structure: ports, sequence numbers, flags and window
On this page

TCP (Transmission Control Protocol) delivers data reliably and in order between two programs, for example a browser and a web server. Every TCP segment starts with a header of at least 20 bytes that tells the receiver which application the data belongs to, where it fits in the stream, and what to do next.

The TCP header fields

FieldSizePurpose
Source port16 bitsPort of the sending application
Destination port16 bitsPort of the receiving application (443 for HTTPS, 22 for SSH)
Sequence number32 bitsPosition of the first data byte in the stream
Acknowledgment number32 bitsNext byte the sender expects to receive (valid when ACK is set)
Data offset (header length)4 bitsHeader size in 32-bit words (5 = 20 bytes, up to 60 bytes)
Reserved3 bitsUnused, must be zero
Flags9 bitsControl bits (below)
Window size16 bitsHow many bytes the receiver can accept (flow control)
Checksum16 bitsError check over header, data and a pseudo-header
Urgent pointer16 bitsMarks urgent data when URG is set (rarely used)
Options0 to 40 bytesMSS, window scaling, SACK, timestamps

TCP flags

FlagMeaning
SYNStart a connection, synchronise sequence numbers
ACKThe acknowledgment number is valid
FINSender has finished sending
RSTReset the connection immediately
PSHPush data to the application without waiting
URGUrgent pointer is valid
ECE, CWRExplicit congestion notification
NSExperimental ECN nonce (obsolete)

The three-way handshake

  1. Client to server: SYN, with the client’s initial sequence number.
  2. Server to client: SYN, ACK, with the server’s sequence number and an acknowledgment of the client’s.
  3. Client to server: ACK. The connection is open.

Closing usually uses FIN and ACK in each direction; RST aborts a connection, and is also what a closed port sends back to a SYN, which is how Nmap scan types such as the SYN scan decide whether a port is open.

Common options

OptionPurpose
MSS (maximum segment size)Largest data payload per segment, often 1460 bytes on Ethernet
Window scaleMultiplies the 16-bit window for fast, high-latency links
SACK permitted / SACKSelective acknowledgments of out-of-order data
TimestampsRound-trip time measurement and protection against old duplicates

Seeing it in Wireshark

Capture any web traffic and filter tcp.flags.syn == 1 to see handshakes, or tcp.analysis.retransmission to find network problems. Wireshark shows relative sequence numbers by default to make them easier to read. More filters are in the Wireshark filters cheat sheet.

Frequently asked questions

How big is the TCP header?

20 bytes without options, and up to 60 bytes with options.

What is the difference between the sequence and acknowledgment numbers?

The sequence number marks where this segment’s data starts; the acknowledgment number tells the other side which byte is expected next.

What does the RST flag mean?

Reset: the connection is aborted, or the port is closed.

What is the TCP window?

The amount of data the receiver is ready to accept, used for flow control.

What is the difference between TCP and UDP headers?

UDP’s header is only 8 bytes (ports, length, checksum) and has no sequence numbers, acknowledgments or connection setup.

Learn networking for security

Our Ethical Hacking and Network Reconnaissance course explains TCP/IP from the packet up and how scanners and attacks use it.

Written by

Taifun Mamedov

Founder and instructor at Ocsaly Academy.

Courses by Taifun Mamedov

Share this article