gray and black laptop computer on surface

TCP Three-Way Handshake: SYN, SYN-ACK and ACK Explained

The TCP three way handshake is the three-packet exchange that opens every TCP connection: the client sends a SYN, the server answers with a SYN-ACK, and the client confirms with an ACK. Those three segments agree the starting sequence numbers for both directions and a handful of options, and only then can any data flow.

Every web page, SSH session and database query over TCP starts this way, so knowing what normal looks like makes it much easier to spot what’s wrong when a connection hangs, gets reset or never opens.

TCP at a glance

  • Layer: transport (layer 4), carried inside IP as protocol number 6.
  • Ports: 16-bit source and destination ports, 0-65535. The server listens on a well-known port (80, 443, 22, 25) and the client picks an ephemeral one (32768-60999 by default on Linux).
  • Standard: RFC 9293 (2022), which replaced the original RFC 793. Options come from RFC 7323 (window scale and timestamps) and RFC 2018 (SACK).
  • Used for: anything that needs a reliable, ordered byte stream: HTTP/1.1 and HTTP/2, TLS, SSH, SMTP, IMAP, database protocols.

How the TCP three way handshake works, step by step

TCP three-way handshakeThe server is in LISTEN. The client sends SYN with its initial sequence number x and enters SYN-SENT. The server enters SYN-RECEIVED and replies SYN-ACK with its own sequence number y and ack x+1. The client enters ESTABLISHED and sends ACK with seq x+1 and ack y+1. The server enters ESTABLISHED. The client sends its first data segment.ClientServerLISTENSYN seq=xSYN-SENTSYN-RECEIVEDSYN-ACK seq=y ack=x+1ESTABLISHEDACK seq=x+1 ack=y+1ESTABLISHEDData seq=x+1 ack=y+1
The client and server swap initial sequence numbers in three segments, then the first data can flow.
  1. The server has called listen() on a port, so its socket is in the LISTEN state, waiting for a SYN.
  2. The client picks a random 32-bit initial sequence number (call it x) and sends a segment with only the SYN flag set and seq=x. It moves to SYN-SENT. The SYN also carries options: maximum segment size, window scale, SACK permitted and usually timestamps.
  3. The server picks its own initial sequence number y, moves to SYN-RECEIVED, and replies with both SYN and ACK set: seq=y, ack=x+1. The ack is x+1 because a SYN uses up one sequence number even though it carries no data. The server adds its own options.
  4. When the SYN-ACK arrives the client is ESTABLISHED. It sends a plain ACK with seq=x+1 and ack=y+1.
  5. When that ACK arrives the server is ESTABLISHED too, and the kernel hands the connection to the application’s accept() call.
  6. The client’s first data segment starts at seq=x+1. In practice the client often sends data straight after the ACK, so the request goes out without waiting another round trip.

Why three packets and not two? Each side has to tell the other its starting sequence number and hear it acknowledged. The SYN-ACK does two jobs at once (acknowledges x, announces y), so three segments cover four pieces of work.

Inside the segment: the TCP header

TCP headerThe 20-byte fixed TCP header, followed by up to 40 bytes of options such as MSS, window scale, SACK and timestamps.08162432Source portDestination portSequence numberAcknowledgment numberOffsetRsrvdFlagsWindowChecksumUrgent pointerOptions (0-40 bytes)
The 20-byte fixed TCP header, followed by up to 40 bytes of options such as MSS, window scale, SACK and timestamps.

The fixed part of the header is 20 bytes. The fields that matter during the handshake are:

  • Sequence number (32 bits): in a SYN this is the initial sequence number. RFC 6528 says it must be hard to guess, which stops blind attackers injecting segments into your connection.
  • Acknowledgment number (32 bits): the next byte the sender expects. Only meaningful when the ACK flag is set, so it’s zero in the first SYN.
  • Data offset (4 bits): header length in 32-bit words, from 5 (20 bytes) to 15 (60 bytes). Handshake segments are often 32 to 40 bytes because of the options.
  • Flags (8 bits): CWR, ECE, URG, ACK, PSH, RST, SYN, FIN. The handshake uses SYN, then SYN+ACK, then ACK.
  • Window (16 bits): how many bytes the sender can receive. The window scale option, which is only allowed in SYN segments, multiplies this by up to 214 for the rest of the connection.
  • Options: MSS (largest segment the sender wants to receive, 1460 on Ethernet with IPv4), window scale, SACK permitted and timestamps. Anything not agreed in the handshake can’t be switched on later.

Here’s a real-looking handshake to port 80, captured with tcpdump -n on the client:

14:02:11.120334 IP 192.0.2.10.51514 > 203.0.113.80.80: Flags [S], seq 3405691582, win 64240, options [mss 1460,sackOK,TS val 2219834 ecr 0,nop,wscale 7], length 0
14:02:11.135901 IP 203.0.113.80.80 > 192.0.2.10.51514: Flags [S.], seq 1879048192, ack 3405691583, win 65160, options [mss 1460,sackOK,TS val 98123456 ecr 2219834,nop,wscale 7], length 0
14:02:11.135950 IP 192.0.2.10.51514 > 203.0.113.80.80: Flags [.], ack 1, win 502, options [nop,nop,TS val 2219850 ecr 98123456], length 0

In tcpdump’s flag notation S is SYN and . is ACK, so [S.] is the SYN-ACK. Here x is 3405691582, and the server’s ack of 3405691583 is x+1. The third line shows ack 1 because tcpdump switches to relative sequence numbers once it has seen both initial values (add -S for absolute ones). The window of 502 in the last segment is scaled: with wscale 7 it means 502 × 128 = 64,256 bytes. The 15.6 ms gap between the SYN and the SYN-ACK is one round trip to the server.

See it on your own network

Start a capture that only shows segments with SYN or RST set, then open a connection from another terminal:

sudo tcpdump -n -i eth0 'tcp[tcpflags] & (tcp-syn|tcp-rst) != 0'
curl -v http://example.com/
nc -vz example.com 443
ss -tan state syn-sent

ss lists sockets stuck in SYN-SENT, which is a quick way to spot connections that are waiting on a server that never answers. Netcat is handy for opening both ends of a connection yourself so you can watch the states change.

In Wireshark the useful display filters are tcp.flags.syn == 1 && tcp.flags.ack == 0 (new connection attempts only), tcp.flags.reset == 1 and tcp.analysis.retransmission. Wireshark also shows relative sequence numbers by default and puts the handshake round-trip time under tcp.analysis.initial_rtt. If you haven’t used it much, start with this guide to packet analysis with Wireshark.

What goes wrong

A lost SYN is retransmittedThe client sends SYN with seq x but it is lost on the way. The client waits for the retransmission timeout, about one second. It sends the same SYN again with the same seq x. The server replies SYN-ACK with seq y and ack x+1, and the client completes the handshake with ACK.ClientServerSYN seq=xno reply, wait RTO=1sSYN (retry) seq=xSYN-ACK seq=y ack=x+1ACK seq=x+1 ack=y+1
When the SYN is dropped nothing comes back, so the client waits for its retransmission timer and sends the same SYN again.

No reply at all. A firewall that silently drops packets gives you a SYN and then retransmissions of the same SYN with the same sequence number, as in the animation. Linux waits 1 second, then doubles the wait each time, and gives up after 6 retries by default (net.ipv4.tcp_syn_retries), which is roughly two minutes. The application sees a timeout (Connection timed out).

RST straight back. If nothing is listening on the port, the server’s kernel answers the SYN with a segment flagged [R.]. The client gets Connection refused immediately. Some firewalls send a reset on purpose to make a filtered port look closed.

The handshake works but data doesn’t. Small handshake packets get through but full-size segments vanish. That usually points to an MTU problem on a tunnel or PPPoE link where ICMP is blocked, so path MTU discovery fails. Clamping the MSS on the router fixes it.

SYN floods. An attacker sends lots of SYNs from spoofed addresses and never completes the handshake, filling the server’s queue of half-open connections in SYN-RECEIVED. Linux defends against this with SYN cookies (net.ipv4.tcp_syncookies), which encode the connection state into the server’s initial sequence number so nothing has to be stored until the final ACK arrives.

Scanners use the handshake too. A SYN scan sends the SYN, reads SYN-ACK as open and RST as closed, then resets instead of completing the connection. The options and window sizes in a SYN-ACK also differ between operating systems, which is how Nmap’s OS detection and p0f fingerprint hosts.

Closing a connection is a separate exchange: each side sends a FIN and acknowledges the other’s, and whichever side closed first sits in TIME-WAIT for a while afterwards. A RST tears the connection down at once.

Try it in Protocol Explorer

Protocol Explorer’s TCP lesson animates the handshake packet by packet in the browser, with a byte-level decoder that shows each header field as it changes and an exam mode for testing yourself. The HTTP and HTTPS lessons are free and the rest, including TCP, come with a one-time unlock.

More in this series

This post is part of How network protocols work, which starts with an overview of layers and encapsulation. Next up is TCP vs UDP, which looks at what you gain and lose by skipping the handshake altogether. TCP also carries the TLS handshake and HTTP, both later in the series.


Leave a Reply