Fundamentals 8 min read

Mastering TCP: The Three-Way Handshake and Four-Way Termination Explained

This article breaks down the TCP three‑way handshake and four‑way termination processes, explaining each packet exchange, the role of sequence numbers, the TIME_WAIT state, and why these steps are essential for reliable full‑duplex communication.

360 Zhihui Cloud Developer
360 Zhihui Cloud Developer
360 Zhihui Cloud Developer
Mastering TCP: The Three-Way Handshake and Four-Way Termination Explained

TCP Three‑Way Handshake

The handshake establishes a reliable full‑duplex connection between a client ("小客客") and a server ("小服服"). It consists of three steps:

Client sends a SYN packet to the server, indicating a request to connect.

Server replies with a SYN‑ACK packet, acknowledging the request and providing its own sequence number.

Client sends an ACK packet, confirming receipt of the server's SYN‑ACK. At this point the connection is established.

The exchange ensures both sides agree on initial sequence numbers, which are used to order and verify subsequent data packets.

TCP three‑way handshake diagram
TCP three‑way handshake diagram

TCP Four‑Way Termination

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

Client sends a FIN packet, indicating it has finished sending data. The client enters FIN_WAIT_1 .

Server acknowledges with an ACK, entering CLOSE_WAIT ; the client moves to FIN_WAIT_2 .

Server sends its own FIN packet, entering LAST_ACK .

Client acknowledges the server's FIN with an ACK and enters TIME_WAIT before finally moving to CLOSED . The server also transitions to CLOSED after receiving this ACK.

The TIME_WAIT state lasts for twice the Maximum Segment Lifetime (MSL) to ensure any delayed packets are discarded and to prevent old packets from being misinterpreted by a new connection on the same IP:port.

TCP four‑way termination diagram
TCP four‑way termination diagram

Key Concepts

Sequence Number : Initial number used to order data segments.

Full‑duplex : Both sides can send and receive simultaneously; therefore both must close their sending direction.

TIME_WAIT purpose : Guarantees old duplicate segments disappear and protects against accidental reuse of the same socket.

Understanding these mechanisms is essential for network programming, troubleshooting connection issues, and designing reliable communication systems.

TCPthree-way handshakeTIME_WAITnetwork protocolfour-way terminationFull-duplex
360 Zhihui Cloud Developer
Written by

360 Zhihui Cloud Developer

360 Zhihui Cloud is an enterprise open service platform that aims to "aggregate data value and empower an intelligent future," leveraging 360's extensive product and technology resources to deliver platform services to customers.

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.