Loading

What TCP's three-way handshake does before a single byte of data is sent

SYN, SYN-ACK, ACK: how two machines agree on a conversation, and what the numbers are for

What TCP's three-way handshake does before a single byte of data is sent

Every web page, mail message and file transfer on a TCP connection begins with the same three packets. The handshake is not a formality: it is how each side learns that the other is listening, and where each side chooses the number that will keep the bytes in order.

Key points

  1. A TCP connection opens with three packets: the client's SYN, the server's SYN-ACK and the client's final ACK.
  2. TCP was first specified in RFC 793 in 1981; its current consolidated specification is RFC 9293, published in 2022.
  3. Each side picks an initial sequence number in its SYN, which RFC 9293 requires to be unpredictable, following RFC 6528.
  4. The SYN packets carry the connection's options: maximum segment size, selective acknowledgement, window scaling and timestamps.
  5. The handshake costs one full round trip before data flows; TCP Fast Open (RFC 7413) and QUIC (RFC 9000) reduce that cost.

The Transmission Control Protocol, TCP, carries most of the internet's traffic that must arrive complete and in order: web pages, mail, file transfers, remote terminals. Unlike the raw IP packets beneath it, which can be lost, duplicated or reordered, TCP presents the two programs at either end with a reliable stream of bytes. Before that stream can exist, the two ends must set it up: that is the job of the three-way handshake.

TCP was specified in 1981 in RFC 793 by the Internet Engineering Task Force's predecessors, and its current consolidated specification is RFC 9293, published in 2022.

Three packets

The client, the side that wants to talk, sends a packet with the SYN flag set, short for synchronise. The server, if it is listening on that port, replies with a packet carrying both the SYN and the ACK flags. The client then sends a final ACK. After those three packets both sides consider the connection established and either may send data. RFC 9293 describes each side's progress through this exchange as a series of states: the server waits in LISTEN, moves to SYN-RECEIVED on the first packet, and reaches ESTABLISHED on the third; the client passes from SYN-SENT to ESTABLISHED on the second.

The handshake answers two questions that neither side can answer alone. Is anyone there? A SYN that receives no reply, after a few retransmissions, tells the client that the port is closed or the host unreachable. And is the reply genuine? Because the server's SYN-ACK must arrive at the address the client wrote in its SYN, a client that lied about its address never sees the reply and cannot complete the third step.

What numbers does the handshake synchronise?

What is actually synchronised is a pair of sequence numbers. Every byte a TCP sender transmits is numbered, and the receiver acknowledges bytes by number, which is how lost segments are detected and retransmitted and how out-of-order segments are put back in order. Each side picks an initial sequence number in its SYN, and the other side's SYN-ACK or ACK confirms it by acknowledging that number plus one. RFC 9293 requires the initial number to be chosen unpredictably, following RFC 6528, so that an attacker who cannot see the traffic cannot guess the numbers and inject data into someone else's connection.

The SYN packets also carry the connection's options: the maximum segment size each side will accept, whether selective acknowledgement is supported, the window scaling factor that lets a connection keep more data in flight than the 65,535 bytes the original 16-bit window field allows, and timestamps used to measure round-trip time. These can only be negotiated at the start, which is one reason the handshake cannot be skipped.

The cost of the handshake

The three packets cost one full round trip before any data flows: on a connection between continents that can be a tenth of a second or more. For the web this is paid on every new connection and then again for the TLS handshake on top. Two responses to this cost are in use. TCP Fast Open, RFC 7413, lets a client that has talked to a server before send data in its SYN, using a cookie the server issued earlier. And QUIC, RFC 9000, the transport under HTTP/3, folds the transport and cryptographic handshakes into one round trip, or none for a repeat visit, by running over UDP and doing its own reliability.

Frequently asked questions

What are the three packets of the TCP handshake?

The client sends a packet with the SYN flag set; the server, if it is listening on that port, replies with a packet carrying both SYN and ACK; the client then sends a final ACK. After those three packets both sides consider the connection established and either may send data.

Why does TCP need a handshake at all?

It tells the client whether anyone is listening, confirms that the reply is genuine because it must reach the address the client gave, synchronises the two sides' sequence numbers, and negotiates options such as segment size and window scaling, which can only be agreed at the start.

Why are TCP initial sequence numbers unpredictable?

RFC 9293 requires the initial sequence number to be chosen unpredictably, following RFC 6528, so that an attacker who cannot see the traffic cannot guess the numbers and inject data into someone else's connection.

How do TCP Fast Open and QUIC speed up connections?

TCP Fast Open, RFC 7413, lets a client that has talked to a server before send data in its SYN, using a cookie issued earlier. QUIC, RFC 9000, the transport under HTTP/3, runs over UDP and folds the transport and cryptographic handshakes into one round trip, or none for a repeat visit.

Sources