TCP Basics

What is the format of the TCP header?

The TCP header is at least 20 bytes and holds ports, sequence numbers, flags, window size, and a checksum. Here is what each field does and why.

On this page
  1. The short answer
  2. The fields
  3. Flags in practice
  4. Data offset and options
  5. Seeing the header

The short answer

A TCP header is at least 20 bytes long. It follows the IP header in each packet, and it carries the source and destination ports, the sequence and acknowledgment numbers, the control flags, the window size, a checksum, and an urgent pointer. Optional fields, when present, come after these. The application data follows the header.

The fields

Field Size What it does
Source port 16 bits The port on the sending side
Destination port 16 bits The port on the receiving side
Sequence number 32 bits Position of this segment’s data in the byte stream
Acknowledgment number 32 bits The next byte the sender expects from the other side, valid when ACK is set
Data offset 4 bits The length of the header, in 32-bit words
Reserved A few bits Kept for future use and set to zero
Flags Several bits Control bits such as SYN, ACK, FIN, RST, PSH, and URG
Window size 16 bits How many bytes the sender of this segment is willing to receive
Checksum 16 bits Error check over the header, the data, and some IP address details
Urgent pointer 16 bits Only meaningful when URG is set
Options Variable Extras such as maximum segment size and window scaling

Flags in practice

  • SYN starts a connection as part of the handshake.
  • ACK means the acknowledgment number is valid. Almost every segment after the first SYN has it set.
  • FIN means the sender has finished sending.
  • RST resets the connection, ending it at once.
  • PSH asks the receiver to pass the data to the application without waiting for more.
  • URG says the urgent pointer is in use. Modern applications rarely use it.

A few more bits in the flags area support explicit congestion notification, which lets routers signal congestion without dropping packets.

Data offset and options

The data offset counts 32-bit words, so the header length is always a multiple of 4 bytes. A value of 5 means the header is 20 bytes with no options. The largest value, 15, means 60 bytes, which is the biggest a TCP header can be.

Common options include:

  • Maximum segment size (MSS), the largest segment the sender will accept. On Ethernet with a standard 1500-byte MTU, IPv4 connections usually use an MSS of 1460 bytes.
  • Window scale, which lets the window size go past 65,535 bytes.
  • SACK, which tells the sender exactly which blocks of data arrived, so it only resends the gaps.
  • Timestamps, which help measure round trips and guard against wrapped sequence numbers.

Seeing the header

Wireshark decodes each field when you select a packet, and it is free and open source. For tools that show headers in a terminal, what are the best networking tools for linux covers the options.

On Linux, tcpdump prints a short summary. Flags appear in square brackets: [S] for SYN, [S.] for SYN-ACK, [.] for an ACK with no flags of its own, [P.] for data pushed with an ACK, [F.] for FIN with ACK, and [R] for RST. A capture of a single HTTPS connection can be started with tcpdump -nn -i any 'tcp port 443'.

The complete guide to tcp basics

TCP Services in Computer Networks
Read the guide

Keep exploring

Guides from other topics

All topics →