The TCP Three-Way Handshake, and How to Watch It Live

What SYN, SYN-ACK and ACK actually mean, why sequence numbers matter, and how to see the whole handshake on your own machine with ss and tcpdump.

· 2 min read · beginner

Every reliable connection on the internet — every web page, every SSH session, every API call — starts with the same three-packet ritual. Understand it once and a huge amount of networking stops being mysterious.

Why a handshake at all?

TCP promises reliable, ordered delivery over an unreliable network. To do that, both sides must agree on a starting point before any data flows: an initial sequence number each will use to number its bytes. The handshake is how they exchange and acknowledge those numbers.

The three packets

text
Client                          Server
  │   ── SYN  seq=x ──────────▶  │   "let's talk; my bytes start at x"
  │   ◀── SYN-ACK seq=y ack=x+1 ─│   "ok; mine start at y, got your x"
  │   ── ACK ack=y+1 ─────────▶  │   "got your y; we're connected"
  │                              │
  │   ===== data flows =====     │
  1. SYN — the client picks a random initial sequence number x and sends a segment with the SYN flag set.
  2. SYN-ACK — the server picks its own y, acknowledges the client's x (by sending ack = x+1), and sets both SYN and ACK flags.
  3. ACK — the client acknowledges the server's y with ack = y+1. The connection is now ESTABLISHED on both ends.

The +1 is because SYN itself consumes one sequence number, even though it carries no data.

See it yourself

Watch a handshake happen in real time. In one terminal, start a capture filtered to one host and port:

bash
sudo tcpdump -n -i any 'tcp port 443 and host example.com'

In another, make a single connection:

bash
curl -s -o /dev/null https://example.com

You will see the three segments go by, flagged [S], [S.] and [.] — tcpdump's shorthand for SYN, SYN-ACK and ACK.

Check connection states without a capture using ss:

bash
ss -tan state syn-sent     # connections mid-handshake
ss -tan state established   # fully open

Connection teardown (the four-way goodbye)

Closing is the handshake's mirror image, but it takes four steps because each direction is shut independently with FIN/ACK. A gracefully closing socket passes through FIN-WAIT, CLOSE-WAIT and finally TIME-WAIT — the deliberate pause that lets any straggling packets drain before the port is reused.

bash
ss -tan state time-wait | head   # sockets waiting out the drain

Common things that go wrong

  • No SYN-ACK, repeated SYNs → nothing is listening, or a firewall is silently dropping. (A firewall that rejects would send an RST instead.)
  • Immediate RST → the port is closed and the host is actively refusing.
  • Handshake completes but TLS stalls → the TCP layer is fine; look higher up.

Takeaways

  • SYN → SYN-ACK → ACK synchronises a random sequence number in each direction.
  • Sequence/ack numbers are how TCP guarantees order and reliability.
  • tcpdump shows the packets, ss shows the resulting states — reach for them before guessing.

Related

Networking beginner

DNS, From Query to Answer

How a name becomes an address — resolvers, the root/TLD/authoritative hierarchy, the record types that matter, and why DNS changes take time.

· 2 min read
Networking beginner

Subnetting Without a Calculator

CIDR notation demystified. Learn the handful of powers of two that let you split and size networks in your head, with a reference table you'll actually remember.

· 1 min read