TCP Three-Way Handshake and Four-Way Termination Explained
This article explains how TCP ensures reliable connections through the three-way handshake process—including SYN, SYN‑ACK, and ACK exchanges—and describes the four-step termination sequence with FIN and ACK flags, while also clarifying why three handshakes are necessary instead of two.
Three‑Way Handshake
The client (active opener) and server (passive opener) both start in the CLOSED state and create a Transmission Control Block (TCB). The server moves to LISTEN, waiting for a connection request.
First handshake : The client sends a SYN segment (SYN=1, ACK=0, seq=x) and enters SYN‑SENT.
Second handshake : The server replies with SYN=1, ACK=1, seq=y, ack=x+1, entering SYN‑RCVD.
Third handshake : The client acknowledges with ACK=1, seq=x+1, ack=y+1, moving both sides to ESTABLISHED.
Why three handshakes? The extra round‑trip prevents a stale SYN (lost request that is later retransmitted) from being mistakenly accepted by a server that has already closed the connection, which would waste resources.
Four‑Way Termination
The termination requires four steps because TCP connections are bidirectional.
First step : The side that wants to close (A) sends FIN=1, seq=u and enters FIN‑WAIT‑1.
Second step : The peer (B) acknowledges with ACK=1, seq=v, ack=u+1, entering CLOSE‑WAIT.
Third step : After B finishes sending data, it sends its own FIN=1, ACK=1, seq=w, ack=u+1 and moves to LAST‑ACK.
Fourth step : A acknowledges the FIN with ACK=1, entering TIME‑WAIT for 2 MSL to ensure B receives the final ACK; then both sides transition to CLOSED.
The TIME‑WAIT state guarantees that any lost final ACK can be retransmitted by B; without it, B might wait indefinitely, leading to resource leakage.
IT Architects Alliance
Discussion and exchange on system, internet, large‑scale distributed, high‑availability, and high‑performance architectures, as well as big data, machine learning, AI, and architecture adjustments with internet technologies. Includes real‑world large‑scale architecture case studies. Open to architects who have ideas and enjoy sharing.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.