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 │
- Step 1 —
SYN(Client → Server): The client chooses a random Initial Sequence Numberx, sets theSYNcontrol flag, sendsSEQ = x, and transitions fromCLOSEDtoSYN_SENT. - Step 2 —
SYN-ACK(Server → Client): The server (listening inLISTEN) receives theSYN, allocates connection state, chooses its own random ISNy, and replies with bothSYNandACKflags set:SEQ = y, ACK = x + 1. The server entersSYN_RCVD(SYN_RECEIVED). - Step 3 —
ACK(Client → Server): The client receives theSYN-ACK, transitions toESTABLISHED, and sendsSEQ = x + 1, ACK = y + 1. When the server receives thisACK, it also transitions toESTABLISHED. 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:
- Announce their own Initial Sequence Number (
xandy), and - 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
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
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?
The SYN flag consumes 1 sequence number, so the server expects sequence number 43 next.
What TCP state is the server in immediately after sending SYN-ACK and before receiving the client's final ACK?
The server transitions from LISTEN to SYN_RCVD upon receiving SYN, and to ESTABLISHED only after the client's ACK arrives.
Why are TCP Initial Sequence Numbers (ISNs) randomized rather than always starting at 0?
Random ISNs isolate incarnations of connections on the same ports and prevent blind spoofing.
Why is a two-way handshake (SYN followed by SYN-ACK) insufficient for TCP?
Both directions of the full-duplex stream must have their starting sequence numbers acknowledged.