Fundamentals 5 min read

Why TCP Uses a Three‑Way Handshake and a Four‑Step Teardown

This article explains TCP fundamentals, detailing the three‑way handshake and four‑step connection termination, the purpose of each flag and state transition, and why three handshakes and four teardowns are required, including the role of the TIME_WAIT state.

Linux Cloud-Native Ops Stack
Linux Cloud-Native Ops Stack
Linux Cloud-Native Ops Stack
Why TCP Uses a Three‑Way Handshake and a Four‑Step Teardown

1. What is TCP

TCP (Transmission Control Protocol) guarantees reliable data delivery: the data will reach the receiver, arrive in order, not be duplicated, and remain intact.

SYN (synchronization flag): indicates a request to establish a connection.

ACK (acknowledgment flag): confirms receipt of a segment.

ISN (initial sequence number): a random number for each connection to prevent attacks.

ack (acknowledgment number): the next expected byte = received sequence number + 1.

2. Three‑Way Handshake

Step 1 – Client → Server: SYN=1, seq=x. Client state changes from CLOSED to SYN_SENT; server state changes from LISTEN to SYN_RCVD. The client tells the server it wants to open a connection with initial sequence x.

Step 2 – Server → Client: SYN=1, ACK=1, ack=x+1, seq=y. Both client and server move to ESTABLISHED. The server acknowledges the client’s SYN (x+1) and supplies its own initial sequence y.

Step 3 – Client → Server: ACK=1, ack=y+1, seq=x+1. Server moves from SYN_RCVD to ESTABLISHED. The client acknowledges the server’s sequence, completing the connection.

3. Four‑Step Teardown

Step 1 – Client → Server: FIN=1, seq=u. Client state: ESTABLISHED → FIN_WAIT_1. The client indicates it has no more data to send.

Step 2 – Server → Client: ACK=1, ack=u+1, seq=v. Client state: FIN_WAIT_1 → FIN_WAIT_2; Server state: ESTABLISHED → CLOSE_WAIT. The server acknowledges the client’s FIN but may still have data to send.

Step 3 – Server → Client: FIN=1, ACK=1, ack=u+1, seq=w. Server state: CLOSE_WAIT → LAST_ACK. The server signals that it has finished sending data.

Step 4 – Client → Server: ACK=1, ack=w+1, seq=u+1. Client state: FIN_WAIT_2 → TIME_WAIT; Server state: LAST_ACK → CLOSED. The client acknowledges the server’s FIN and then waits 2 MSL before closing.

4. Why Three Handshakes?

Prevents a stale connection request from creating a false connection and wasting resources.

Ensures both sides can confirm that the other’s send/receive capabilities are functional.

5. Why Four Steps?

TCP is full‑duplex; each direction’s sending channel must be closed independently.

The server may still have data to send after receiving the client’s FIN, so it must ACK the FIN first, then send its own FIN after finishing transmission.

6. Role of TIME_WAIT

Guarantees the final ACK reaches the peer; if lost, the server will retransmit its FIN after timeout.

Prevents old duplicate segments from being misinterpreted by a new connection, avoiding data corruption.

Three‑Way Handshake Diagram
Three‑Way Handshake Diagram
Four‑Step Teardown Diagram
Four‑Step Teardown Diagram
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.

TCPthree-way handshakeTIME_WAITnetwork fundamentalsfour-step termination
Linux Cloud-Native Ops Stack
Written by

Linux Cloud-Native Ops Stack

Focused on practical internet operations, sharing server monitoring, troubleshooting, automated deployment, and cloud-native tech insights. From Linux basics to advanced K8s, from ops tools to architecture optimization, helping engineers avoid pitfalls, grow quickly, and become your tech companion.

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.