awesome-everything RU
↑ Back to the climb

Networking & Protocols

The three-way handshake

Crux TCP turns two strangers on the network into a reliable two-way conversation — the handshake is the three-step ritual that starts it.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at junior altitude — the surface
◷ 10 min

You opened this page and a stranger on another continent agreed to talk to you. Before any HTTP request, before TLS, before DNS resolved — TCP did three things: said hello, agreed on starting sequence numbers, and confirmed the agreement. That is the handshake.

What TCP does in one sentence

TCP turns two strangers on the network into a reliable two-way conversation: every byte arrives in order, every byte is acknowledged, and missing bytes are resent. The handshake is how the conversation starts.

The IP layer moved packets across networks, but it did not care if they arrived, arrived in order, or arrived at all. TCP adds a contract: every byte is numbered, every byte is acknowledged, and if one is missing it is retransmitted. That contract is negotiated in the handshake. Before the handshake, both sides are strangers. After it, both sides carry state: buffers, windows, and the agreement to talk until one of them says goodbye.

Why care

Without TCP every web request would have to manage its own retries and ordering. TCP gives every higher protocol (HTTP, IMAP, SSH, anything that runs over it) a clean reliable pipe for free.

The phone-call metaphor

Imagine two people on a phone call. Before they say anything important they exchange “hello — can you hear me? — yes, I can hear you, can you hear me? — yes.” That short three-step ritual is the TCP handshake.

  • SYN = “hello, here is my counter”
  • SYN-ACK = “I hear you, here is my counter, and I confirm I heard your counter”
  • ACK = “I confirm your counter, let’s begin”

Now both phones agree they are on the same line and can start the real conversation.

The handshake in plain dialogue

Bea dials Sven and says “Hello, my counter starts at 42,000,000.” Sven picks up: “Got it, your next byte should be 42,000,001; my counter starts at 99,000,000.” Bea: “Got it, your next byte should be 99,000,001, let’s go.” From this moment every word both of them say is numbered so neither can lose track.

Three-way handshake at a glance
Step 1 (client→server)
SYN seq=X
Step 2 (server→client)
SYN-ACK seq=Y ack=X+1
Step 3 (client→server)
ACK seq=X+1 ack=Y+1
State after step 3
ESTABLISHED on both sides
Cost
1 full round-trip (RTT)
LAN RTT
0.1–1 ms

Why three steps and not two

TCP is bidirectional and both sides must agree on each other’s starting sequence numbers. A two-step handshake (SYN then data) would leave the server’s sequence number unacknowledged. The third step (ACK) proves the client is alive and has read the server’s reply. It also bounds half-open connections: if a SYN arrives but no ACK follows, the server times out the half-open entry and frees memory.

One scenario end to end

You click a link in your browser. Underneath, the browser already finished IP routing (it knows the server’s address) and DNS (it resolved the name). Now it does the TCP handshake you just learned. Once finished, the browser layers TLS on top to seal the connection, and only then does the actual HTTP request travel.

Quiz

Why does TCP need three messages and not two?

Quiz

What guarantee does TCP give once the handshake completes?

Order the steps

Order the three steps of the TCP handshake:

  1. 1 Client sends SYN with its starting sequence number
  2. 2 Server replies with SYN-ACK: its sequence number plus an acknowledgement of the client's
  3. 3 Client sends ACK confirming the server's sequence number
  4. 4 Both sides move to ESTABLISHED and can exchange data
Complete the analogy

Fill in the blank: the TCP handshake is like a _______ where both sides confirm they can hear each other before saying anything important.

Why this works

Why TCP adds statefulness. IP is stateless: each packet is routed independently and the network keeps no memory of previous packets. TCP adds state at both endpoints — buffers, window sizes, sequence counters. This state is what enables reliability. The handshake is the moment that state is created and agreed upon. After it, both sides are no longer strangers.

Recall before you leave
  1. 01
    In one sentence: why does every new TCP connection take at least one round-trip before any data flows?
  2. 02
    What would go wrong if TCP used a two-step handshake (SYN then data) instead of three steps?
  3. 03
    What is the difference between TCP and IP in terms of delivery guarantees?
Recap

TCP builds a reliable ordered byte stream on top of IP’s best-effort packet delivery. The three-way handshake is how the connection starts: the client sends SYN with its initial sequence number, the server replies with SYN-ACK carrying its own sequence number and confirming the client’s, and the client sends ACK confirming the server’s. All three steps are necessary because TCP is bidirectional — both directions must independently acknowledge each other’s starting counter. The cost is one full round-trip, which is 80–300 ms on intercontinental links. Higher-layer protocols (TLS, HTTP) build on this foundation, which is why understanding the handshake is essential for diagnosing latency.

Connected lessons
appears again in152
Continue the climb ↑Sequence numbers and connection state
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources2
expand
  1. 01
  2. 02

Trademarks belong to their respective owners. Editorial reference only.