1. Home
  2. Computer Networks
  3. TCP 3-Way Handshake

TCP 3-Way Handshake

How two computers agree to start a reliable conversation — SYN, SYN-ACK, ACK — plus data acknowledgements and the 4-way close, as a 3D sequence diagram.

Interactive 3DBeginner11 min readCNUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · Left arrowRight arrow step

What's happening

TCP gives reliable, ordered delivery. Before any data flows, the two computers agree on starting sequence numbers with a 3-way handshake. Time flows downward. Press Run.

Pseudocode

  1. 1. client → SYN
  2. 2. server → SYN + ACK
  3. 3. client → ACK (connection open)
  4. data segments are acknowledged with ACK numbers
  5. closing: FIN, ACK, FIN, ACK
Read this model as text · 6 steps

One run of the model above, written out step by step. Useful with a screen reader, on a slow connection, or for quick revision. The values may differ from the run on your screen.

  1. TCP gives reliable, ordered delivery. Before any data flows, the two computers agree on starting sequence numbers with a 3-way handshake. Time flows downward. Press Run.
  2. The server is LISTENing on port 443. The client (your browser) wants to connect.
  3. The client wants to talk. It sends a SYN (synchronise) segment with a random starting sequence number, 100.
  4. The server agrees: SYN-ACK. It acknowledges the client’s number (ack = 100 + 1) and picks its own starting number, 300.
  5. The client acknowledges the server’s number (ack = 301). Both sides now know each other’s sequence numbers: the connection is ESTABLISHED.
  6. Why three messages? Each side must send its own starting sequence number and hear the other side acknowledge it. Two messages would not confirm that the server’s number arrived.

Try this in the 3D model

  • Run the 3-way handshake. How is each ACK number calculated?
  • Choose Handshake + data transfer. After 100 bytes starting at 101, what ACK does the server send?
  • Choose 4-way connection close and follow the state labels on both sides.

Why a handshake?

TCP (Transmission Control Protocol) promises reliable, ordered delivery over an unreliable network. To do that, both sides number every byte they send with sequence numbers, and acknowledge what they receive with ACK numbers.

Before sending data, the two computers must agree on their starting sequence numbers. That agreement is the 3-way handshake.

The three steps

Client                                    Server (LISTEN)
  |  ---- SYN, seq = x ----------------->   |      client: SYN-SENT
  |  <--- SYN-ACK, seq = y, ack = x + 1 --  |      server: SYN-RECEIVED
  |  ---- ACK, seq = x + 1, ack = y + 1 -->  |      both: ESTABLISHED
  1. SYN — “I want to connect; my first sequence number is x.”
  2. SYN-ACK — “OK. I got x (I expect x + 1 next), and my first number is y.”
  3. ACK — “Got y. Let’s go.”

Starting numbers are random, which protects against confusing old, delayed packets with a new connection (and against some attacks).

Why not two steps?

With only SYN and SYN-ACK, the server would never know whether its own starting number y reached the client. Each side needs to send its number and see it acknowledged — that takes three messages.

Data transfer

Every data segment carries a sequence number; the receiver replies with an ACK = next byte expected. If the client sends 100 bytes starting at 101, the server answers ack = 201. Missing ACKs trigger retransmission. To avoid waiting for every single ACK, TCP keeps several segments in flight using a sliding window.

Closing: the 4-way handshake

Each direction is closed separately:

  1. Client → FIN (I’m done sending) → FIN-WAIT-1
  2. Server → ACK → server CLOSE-WAIT, client FIN-WAIT-2
  3. Server → FIN (I’m done too) → LAST-ACK
  4. Client → ACK → client waits in TIME-WAIT (to catch a lost final ACK), then both CLOSED

Key TCP states

State Meaning
LISTEN Server waiting for connections
SYN-SENT / SYN-RECEIVED Handshake in progress
ESTABLISHED Connection open — data can flow
FIN-WAIT, CLOSE-WAIT, LAST-ACK Closing
TIME-WAIT Waiting a short while after closing

See it on your own computer

# Linux / macOS: list TCP connections and their states
netstat -an | grep -E "LISTEN|ESTABLISHED"

A tiny TCP client in Python — the handshake happens inside connect():

import socket
s = socket.create_connection(("example.com", 80))   # SYN, SYN-ACK, ACK happen here
s.sendall(b"GET / HTTP/1.0\r\nHost: example.com\r\n\r\n")
print(s.recv(200))
s.close()                                           # FIN / ACK exchange

TCP vs UDP

TCP UDP
Connection setup 3-way handshake None
Reliability & order ✅ ❌
Speed / overhead Higher overhead Very light
Used for Web, email, file transfer Video calls, gaming, DNS

Common mistakes

  • Forgetting the +1 in ACK numbers (a SYN or FIN counts as one sequence number).
  • Mixing up which side sends the SYN-ACK (always the side that received the SYN).
  • Thinking closing also takes three messages — it usually takes four.

Complexity at a glance

Case / operationTimeWhy
Messages to open a connection3SYN, SYN-ACK, ACK.
Messages to close4FIN, ACK, FIN, ACK.
Delay before data can flow1 round-trip time

Quick check

Test yourself — pick an answer to see if you got it.

1. What is the correct order of the TCP handshake?

2. The client's SYN has seq = 100. What ack number does the server's SYN-ACK carry?

3. Why does TCP need a handshake but UDP does not?

4. Which state does a server wait in before any client connects?

Saved in this browser.
Spotted a mistake or a bug in the 3D model?

Report a mistake

in TCP 3-Way Handshake. Thank you — every report makes the lesson better for the next reader.

We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.