Fundamentals 9 min read

Unveiling TCP’s Three‑Way Handshake and Four‑Way Teardown with Simple Animations

This article uses simple animations and analogies to demystify TCP’s three‑way handshake, data transmission nuances, and the four‑way connection termination, explaining key states like SYN_SENT, SYN_RCVD, and TIME_WAIT while also covering retransmission, duplicate‑ack handling, and window sizing.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Unveiling TCP’s Three‑Way Handshake and Four‑Way Teardown with Simple Animations

TCP three‑way handshake and four‑way termination are common interview topics; this article explains them with animations to make the underlying interaction easier to understand.

1. TCP Three‑Way Handshake

TCP’s three‑way handshake is likened to two people spotting each other from 50 m away in fog and confirming identity by waving.

Person A (Zhang San) first waves ( SYN ). Person B (Li Si) replies with a nod and smile ( ACK ), confirming A’s identity and entering the ESTABLISHED state.

Because B still wants to be sure, B also sends a SYN , and A replies with ACK , merging the two actions into a single SYN+ACK step. Thus the four physical actions reduce to three logical steps: SYN, SYN+ACK, ACK.

The intermediate states are SYN_SENT (active opener’s half‑open) and SYN_RCVD (passive opener’s half‑open).

SYN_SENT: SYN packet has been sent.

SYN_RCVD: SYN packet has been received.

2. TCP Data Transmission

Data transmission is compared to a conversation where each spoken sentence must be acknowledged; loss triggers retransmission.

If an ACK is not heard, the sender assumes the message was lost and retransmits. The network stack handles retransmission and duplicate‑ack removal automatically.

TCP is full‑duplex: both sides can initiate data transfer, but each segment must be acknowledged.

When many segments are sent quickly, the receiver can send a cumulative ACK (batch ACK). The transmission rate is limited by the TCP window size.

Out‑of‑order packets may arrive due to different routing paths; the kernel reorders them before delivering to the application.

3. TCP Four‑Way Termination

Connection teardown mirrors the handshake but usually requires four distinct steps: FIN, ACK, FIN, ACK.

The middle two steps cannot be merged because TCP supports a “half‑close” state: one side can stop sending while still receiving. TIME_WAIT is entered by the active closer after sending the final ACK; it lasts for 2 MSL (≈4 minutes) to ensure the last FIN is retransmitted if necessary and to allow stray packets to disappear.

During TIME_WAIT the socket cannot be reused, preventing conflicts with delayed packets from previous connections.

In some cases the middle two steps are combined, allowing a three‑step termination where the state jumps from FIN_WAIT_1 directly to TIME_WAIT, skipping FIN_WAIT_2.

4. Summary

TCP state transitions are complex; this article covered the basic concepts of the three‑way handshake, data exchange, and four‑way termination using simple analogies. Readers should now have a solid foundation for deeper TCP studies.

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.

TCPNetworkingprotocolConnection terminationHandshake
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

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.