Fundamentals 7 min read

Why Does TCP Need a Three‑Way Handshake? And How the Four‑Way Termination Works

This article explains how TCP establishes reliable connections using a three‑way handshake, why two‑way handshakes are insufficient, and details the four‑step termination process, including the purpose of the TIME‑WAIT state in TCP/IP networks.

Open Source Linux
Open Source Linux
Open Source Linux
Why Does TCP Need a Three‑Way Handshake? And How the Four‑Way Termination Works

1. Three‑Way Handshake

To understand TCP reliability, first know that the client initiates a request and the server listens for connections. Both sides start in the CLOSED state and create a Transmission Control Block (TCB). The server moves to LISTEN after creating its TCB.

First handshake : The client sends a segment with SYN=1, ACK=0, seq=x and enters SYN‑SENT.

SYN=1, ACK=0 indicates a connection request.

x is the initial sequence number; SYN consumes one sequence number.

Second handshake : The server replies with SYN=1, ACK=1, seq=y, ack=x+1 and enters SYN‑RCVD.

SYN=1, ACK=1 indicates acceptance of the connection.

seq=y is the server's initial sequence number.

ack=x+1 tells the client the next expected byte.

Third handshake : The client acknowledges with ACK=1, seq=x+1, ack=y+1, moving to ESTABLISHED; the server also enters ESTABLISHED, completing the connection.

4. Why a Three‑Way Handshake Is Needed

Two‑way handshakes cannot protect against lost connection requests. If a request is lost and the client retransmits, the server might mistakenly consider an old request as new, leading to resource waste or dead connections.

5. TCP Four‑Way Termination

The termination requires four steps because TCP is bidirectional.

First step : A (the initiator) sends FIN=1, seq=u and enters FIN‑WAIT‑1.

FIN=1 signals a release request.

seq=u is the last byte sent by A.

Second step : B acknowledges with ACK=1, seq=v, ack=u+1, entering CLOSE‑WAIT.

ACK=1 acknowledges the FIN.

seq=v is B's last byte.

ack=u+1 indicates B expects the next byte from A.

A moves to FIN‑WAIT‑2, waiting for B's own FIN.

Third step : B sends its FIN=1, ACK=1, seq=w, ack=u+1 and enters LAST‑ACK.

Fourth step : A acknowledges with ACK=1, entering TIME‑WAIT for 2 MSL before closing; B then closes after receiving the ACK.

6. Why A Enters TIME‑WAIT Before CLOSED

A stays in TIME‑WAIT to ensure B receives the final ACK. If A closed immediately and the ACK were lost, B would retransmit its FIN, but A would no longer respond, preventing a graceful shutdown.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

TCPReliability
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

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.