A Visual Guide to TCP’s Three‑Way Handshake and Four‑Way Teardown
This article uses animated analogies to demystify TCP’s three‑way handshake, data transmission nuances, and four‑way connection termination, explaining states like SYN_SENT, SYN_RCVD, ESTABLISHED, and TIME_WAIT, while also covering retransmission, flow control, and packet ordering in an easy‑to‑understand visual format.
TCP three‑way handshake and four‑way termination are common interview topics; this article uses animations and analogies to simplify their concepts.
TCP Three‑Way Handshake
The handshake is likened to two people spotting each other from a distance and confirming identity by waving. Person A (client) sends a syn wave, Person B (server) replies with an ack smile, establishing the ESTABLISHED state.
Person B then sends its own syn , and Person A responds with an ack , merging the two actions into a single syn+ack step. Thus the four actions reduce to three, representing the three‑way handshake.
The intermediate states are syn_sent (client’s half‑open) and syn_rcvd (server’s half‑open).
syn_sent : SYN packet has been sent.
syn_rcvd : SYN packet has been received.
TCP Data Transmission
Data exchange is compared to two people shouting messages across a distance, requiring acknowledgments ( ack ) for each spoken phrase. If an acknowledgment is lost, the sender retransmits the message.
Duplicate receptions are handled by de‑duplication, and the operating system’s network stack manages retransmission and ordering, so user‑level code need not worry.
TCP is full‑duplex, allowing both sides to initiate data transfer, but each segment must be acknowledged. Batch acknowledgments ( bulk ack ) can be used when many messages are received quickly.
Transmission rate is controlled by the TCP window size, and packets may arrive out of order due to different routing paths; the kernel reorders them before delivering to the application.
TCP Four‑Way Teardown
Connection termination mirrors the handshake but involves four steps: fin from the initiator, ack from the peer, fin from the peer, and final ack from the initiator.
The middle two steps cannot be merged because TCP supports a “half‑close” state, allowing one direction to close while the other remains open.
The initiator then enters the
time_waitstate for a standard duration of 4 minutes (2 MSL). This period ensures the final ACK is retransmitted if needed and allows any stray packets in the network to expire, preventing them from interfering with future connections.
time_waitlasts for 2 MSL, where each MSL (Maximum Segment Lifetime) is defined as 2 minutes by the RFC. Occasionally the middle two steps can be combined, resulting in a three‑step termination where the initiator moves directly from
fin_wait_1to
time_wait.
Summary
TCP state transitions are complex; this article covers the basic concepts using visual analogies. Readers are encouraged to explore deeper technical resources to master advanced TCP topics.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
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.