Fundamentals 11 min read

Why Does TCP Need a Three‑Way Handshake? A Simple Analogy Explained

The article uses a humorous long‑distance video‑chat scenario to illustrate why TCP requires a three‑way handshake for connection establishment and a four‑step termination, explaining each packet exchange, flag meanings, and related concepts such as half‑open connections and SYN‑flood attacks.

Efficient Ops
Efficient Ops
Efficient Ops
Why Does TCP Need a Three‑Way Handshake? A Simple Analogy Explained

Background

After more than a year of long‑distance relationship, the author proposes a nightly video call to keep the connection alive, but network glitches often cause the video to freeze and the audio to drop.

Problem

When the network quality deteriorates, both parties repeatedly ask each other, “Can you hear me?” leading to a tedious back‑and‑forth confirmation loop.

Solution

The situation is used as an analogy to explain why TCP establishes a connection with a three‑way handshake and terminates it with a four‑step process.

TCP Three‑Way Handshake

TCP (Transmission Control Protocol) is a reliable transport‑layer protocol (IP protocol number 6). The three‑way handshake ensures both ends agree on initial sequence numbers and that the connection is ready for data transfer.

Phone call analogy
Phone call analogy

The handshake can be visualized as a phone conversation:

Client‑server communication
Client‑server communication

TCP Packet Format

TCP packet format
TCP packet format

Key fields include:

Seq : 32‑bit sequence number identifying the byte stream.

Ack : 32‑bit acknowledgment number (valid only when the ACK flag is set); Ack = Seq + 1.

Flags : URG, ACK, PSH, RST, SYN, FIN – each with a specific meaning.

TCP Flags

URG : urgent pointer is valid.

ACK : acknowledgment number is valid.

PSH : push – deliver data to the application promptly.

RST : reset the connection.

SYN : synchronize – initiate a new connection.

FIN : finish – close a connection.

Three‑Way Handshake Steps

First handshake: Client sends a packet with SYN=1 and a random sequence number J, entering SYN_SENT state.

Second handshake: Server receives the SYN, replies with SYN=1 and ACK=1, ack=J+1, and its own random sequence number K, entering SYN_RCVD state.

Third handshake: Client acknowledges with ACK=1, ack=K+1; both sides transition to ESTABLISHED state, and data transfer can begin.

Three‑way handshake diagram
Three‑way handshake diagram

SYN Attack

During the handshake, the server’s half‑open state (after sending SYN‑ACK but before receiving ACK) can be abused. An attacker floods the server with forged SYN packets from spoofed IP addresses, exhausting the backlog queue and causing a denial‑of‑service.

<code># netstat -nap | grep SYN_RECV</code>

Four‑Way Termination

Closing a TCP connection requires four packet exchanges because the connection is full‑duplex; each direction must be closed independently.

First: Client sends FIN, entering FIN_WAIT_1 .

Second: Server acknowledges with ACK, entering CLOSE_WAIT .

Third: Server sends its own FIN, entering LAST_ACK .

Fourth: Client acknowledges with ACK, entering TIME_WAIT ; server moves to CLOSED .

Four‑way termination diagram
Four‑way termination diagram

If both sides initiate closure simultaneously, the sequence interleaves, but the total number of packets remains four.

Simultaneous close diagram
Simultaneous close diagram

Interview Questions

What is the three‑way handshake and how does it work?

Why does establishing a TCP connection require three steps while terminating it requires four?

TCPnetwork protocolsthree-way handshakefour-way terminationnetworking fundamentalsSYN attack
Efficient Ops
Written by

Efficient Ops

This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.