Labs / Networking

TCP Three-Way Handshake

Step through connection establishment. Watch Sequence numbers and Acknowledgement numbers coordinate state.

New to this? There\'s a lesson below ↓
← BACK TO LABS
Network Simulator
CLIENT
CLOSED
SERVER
LISTEN
Waiting to start...
Packet Details
LAST PACKET
---
SEQ NUM
---
ACK NUM
---
What's happening?

Step 1: SYN
Client wants to connect. Sends a SYN (Synchronize) packet with a random initial sequence number.

Step 2: SYN-ACK
Server acknowledges the client's SYN by adding 1 to the sequence number. Server also sends its own SYN with its own random sequence number.

Step 3: ACK
Client acknowledges the server's SYN by adding 1 to it. The connection is now established and data can flow.

The lesson

TCP Connection Establishment and the Three-Way Handshake

How SYN, SYN-ACK, and ACK synchronize Initial Sequence Numbers (ISNs) across an unreliable IP network to establish a full-duplex connection.

1. Why TCP Needs a Connection Handshake

The Network Layer (IP) is connectionless and best-effort: packets can be dropped, duplicated, delayed, or delivered out of order. The Transport Layer protocol TCP (Transmission Control Protocol) builds a reliable, ordered, byte-stream service on top of IP.

To guarantee in-order delivery and detect lost data, TCP assigns a 32-bit Sequence Number (SEQ) to every byte sent, and the receiver returns cumulative Acknowledgement Numbers (ACK) indicating the next byte sequence number expected.

Before application data can be exchanged, both endpoints must synchronize their starting sequence numbers — called Initial Sequence Numbers (ISNs) — using the Three-Way Handshake.

2. Tracing the Three Messages and State Transitions

Click START HANDSHAKE in the simulator above and step through all three segments while watching the Client/Server states and Packet Details:

Client (CLOSED)                                        Server (LISTEN)
   │                                                          │
   │  1. SYN          SEQ = x                                 │
   ├─────────────────────────────────────────────────────────►│  Server → SYN_RCVD
   │  Client → SYN_SENT                                       │
   │                                                          │
   │  2. SYN-ACK      SEQ = y,  ACK = x + 1                   │
   │◄─────────────────────────────────────────────────────────┤
   │                                                          │
   │  3. ACK          SEQ = x + 1,  ACK = y + 1               │
   ├─────────────────────────────────────────────────────────►│  Server → ESTABLISHED
   │  Client → ESTABLISHED                                    │
  1. Step 1 — SYN (Client → Server): The client chooses a random Initial Sequence Number x, sets the SYN control flag, sends SEQ = x, and transitions from CLOSED to SYN_SENT.
  2. Step 2 — SYN-ACK (Server → Client): The server (listening in LISTEN) receives the SYN, allocates connection state, chooses its own random ISN y, and replies with both SYN and ACK flags set: SEQ = y, ACK = x + 1. The server enters SYN_RCVD (SYN_RECEIVED).
  3. Step 3 — ACK (Client → Server): The client receives the SYN-ACK, transitions to ESTABLISHED, and sends SEQ = x + 1, ACK = y + 1. When the server receives this ACK, it also transitions to ESTABLISHED. Full-duplex data transfer can now begin (and the first HTTP request bytes can even piggyback on this 3rd segment).

3. Key Design Questions

Why does ACK = x + 1 when the SYN segment carried 0 bytes of payload?
In TCP, an ACK number always specifies the next sequence number the receiver expects. The SYN flag (and later the FIN flag at connection close) logically consumes 1 sequence number so that it can be reliably acknowledged and retransmitted if lost.

Why is a 3-way handshake required instead of a 2-way handshake?
A TCP connection is full-duplex (data flows in both directions independently). Both sides must:

  1. Announce their own Initial Sequence Number (x and y), and
  2. Receive confirmation (ACK) that the other side received it.

After Step 2 (SYN-ACK), the client knows the server received x, but the server does not yet know if the client received y. Furthermore, if a delayed duplicate SYN from an old closed connection arrived at the server, a 2-way handshake would cause the server to open an orphaned connection without the client's consent. Step 3 proves the client is live and confirms y + 1.

Why are Initial Sequence Numbers (x and y) chosen randomly instead of starting at 0?

  • Preventing stale packet collisions: If a connection closes and a new connection opens on the same IP/port pair, random ISNs prevent delayed segments from the old connection from falling inside the new connection's sequence window.
  • Security against TCP sequence spoofing: If ISNs were predictable, an off-path attacker could guess the sequence number and inject forged packets (or complete a spoofed handshake).

Exercises

1. A client initiates a connection with ISN = 1000 and the server responds with ISN = 5000. Write the exact Flags, SEQ, and ACK fields for all three handshake segments, plus the server's ACK if the client next sends a 200-byte HTTP request.

Show solution
• Segment 1: Flags = SYN, SEQ = 1000, ACK = —
• Segment 2: Flags = SYN-ACK, SEQ = 5000, ACK = 1001
• Segment 3: Flags = ACK, SEQ = 1001, ACK = 5001
• After 200-byte payload (bytes 1001..1200): Server replies with ACK = 1001 + 200 = 1201.

2. What is a SYN Flood attack, and how do SYN Cookies defend a server against it?

Show solution
In a SYN Flood, an attacker sends thousands of SYN packets with spoofed source IPs and never sends the final Step-3 ACK, filling the server's queue of half-open (SYN_RCVD) connections. With SYN Cookies, the server does not allocate memory on Step 1; instead, it cryptographically encodes the connection parameters into its sequence number y and reconstructs the state only if a valid ACK = y + 1 returns in Step 3.

Quiz

If a client sends a SYN segment with SEQ = 42, what ACK value must the server include in its SYN-ACK response?

What TCP state is the server in immediately after sending SYN-ACK and before receiving the client's final ACK?

Why are TCP Initial Sequence Numbers (ISNs) randomized rather than always starting at 0?

Why is a two-way handshake (SYN followed by SYN-ACK) insufficient for TCP?