LightAtom

The TCP Three-Way Handshake

Every TCP connection starts with three packets, SYN, SYN-ACK and ACK, that agree on sequence numbers before any data flows.

The TCP three-way handshake opens a connection: the client sends SYN, the server answers SYN-ACK, and the client confirms with ACK. Both sides agree on starting sequence numbers, and data can flow after one round trip.

How it works

The client sends SYN with sequence x, the server replies SYN-ACK with sequence y and ack x+1, the client sends ACK y+1, and data flows.

Opening

  1. SYN: the client picks a random initial sequence number and asks to connect.
  2. SYN-ACK: the server acknowledges it and sends its own sequence number.
  3. ACK: the client acknowledges the server's number. The connection is ESTABLISHED.

Sequence numbers let each side put data in order and detect what's missing, which is what makes TCP reliable.

What failures look like

  • No reply to SYN: a firewall dropped it, or the host is unreachable: a connection timed out after retries.
  • RST in reply to SYN: nothing is listening on that port: connection refused.

Closing

Either side sends FIN; the other acknowledges and sends its own FIN. The side that closed first waits in TIME_WAIT for a while to catch stray packets. A RST aborts a connection immediately (connection reset by peer).

Why it matters for speed

The handshake costs a full round trip before data, and TLS adds another. That's why latency matters so much for page loads, and why TLS 1.3, TCP Fast Open and QUIC work to reduce round trips.

Examples

$ sudo tcpdump -i en0 -n 'tcp[tcpflags] & (tcp-syn) != 0 and host 1.1.1.1'
IP 192.168.1.23.53621 > 1.1.1.1.443: Flags [S], seq 1830237467, …
IP 1.1.1.1.443 > 192.168.1.23.53621: Flags [S.], seq 2401195339, ack 1830237468, …

[S] is SYN and [S.] is SYN-ACK. Run curl -sI https://1.1.1.1 in another window to trigger it.

On a Mac

  • netstat -an -p tcp shows every connection's state: SYN_SENT, ESTABLISHED, TIME_WAIT, CLOSE_WAIT.
  • nc -vz host 443 performs just the handshake and reports whether it succeeded.
  • curl -w '%{time_connect}\n' -o /dev/null -s https://example.com prints the handshake time.

See it in the app

LightAtom's connections view shows each app's TCP connections and their state, from the first SYN to close.

Frequently asked questions

What are the three steps of the TCP handshake?

SYN from the client, SYN-ACK from the server, and ACK from the client.

Why does TCP need a handshake?

To agree on starting sequence numbers and confirm both sides can send and receive before any data is exchanged.

Does UDP have a handshake?

No. UDP just sends packets. Protocols built on UDP, like QUIC, add their own handshakes.

What is a SYN flood?

An attack that sends many SYNs without completing the handshake, filling the server's queue of half-open connections. SYN cookies defend against it.

LightAtom team

The people who build LightAtom, a network monitor for macOS.