Understanding TCP: 3-Way Handshake & Reliable Communication
How the Transmission Control Protocol guarantees that your data arrives intact, in order, and without errors across a chaotic internet.

Imagine you want to send a 1,000-page manuscript to a friend across the country.
Now, imagine the postal service has a bizarre rule: you cannot send the manuscript as a single book. You must tear out every single page, stuff each one into its own unnumbered envelope, and drop them into random mailboxes around your city.
What happens when your friend receives them? The pages will arrive out of order. Some envelopes will get lost in transit. Some might get duplicated. Your friend will be left with a chaotic, unreadable pile of paper.
This is exactly how the internet works at its lowest levels. The fundamental routing layer of the internet, the Internet Protocol (IP), is "best-effort." It breaks your data into small packets and shoots them across various routers toward their destination. It makes zero guarantees that the packets will arrive in order, or that they will arrive at all.
If we relied solely on IP, downloading an image would result in a pixelated mess, and loading a webpage would give you half of the HTML tags.
To fix this, we need rules. We need the Transmission Control Protocol (TCP).
TCP sits on top of IP. If IP is the unpredictable mail carrier, TCP is the meticulous shipping manager. It numbers the pages, tracks the deliveries, demands receipts, and resends anything that gets lost. Here is exactly how it does it.
The 3-Way Handshake Establishes Trust
Before TCP sends a single byte of actual data, it requires both the client and the server to establish a formal connection. They have to synchronize their counters and agree to talk.
This is done using the TCP 3-Way Handshake.
Think of it like making a phone call in a noisy room:
You: "Hello, can you hear me?"
Them: "I can hear you! Can you hear me?"
You: "Yes, I can. Let's talk."
In TCP, this conversation happens using special flags attached to the network packets: SYN (Synchronize) and ACK (Acknowledge).
Image by NetworkWalks Academy
Let's break down the steps:
SYN: The client wants to connect. It sends a packet with the SYN flag turned on, and it provides a random starting Sequence Number (let's say 100). This says: "I want to talk, and my starting page number is 100."
SYN-ACK: The server receives this and replies with two flags. It acknowledges the client's request (ACK = 101, meaning "I got 100, I am ready for 101"), and it synchronizes its own starting number (SYN = 500).
ACK: The client receives the server's reply. It sends back a final ACK (= 501), meaning "I got your sequence number 500, I am ready for 501."
The connection is now open. Both sides have a reliable, synchronized channel.
Reliable Data Transfer with Sequence and Acknowledgement
Now that the connection is open, you request a webpage (HTTP runs over TCP). The server starts sending the HTML file.
Because the internet requires data to be broken down into small packets, TCP assigns a Sequence Number to every packet. This solves the "out of order" problem. Even if packet #4 arrives before packet #3, the client's TCP layer simply holds onto #4, waits for #3, and reassembles them in the correct order before handing the data up to the web browser.
To solve the "lost packet" problem, TCP uses Acknowledgements. For every packet the server sends, it expects the client to send an ACK back, acting as a delivery receipt.
[ Normal Data Transfer ]
Client Server
| |
| <------ Data Packet (Sequence = 1) ------------ |
| ------- ACK (Acknowledgement = 2) ------------> |
| |
| <------ Data Packet (Sequence = 2) ------------ |
| ------- ACK (Acknowledgement = 3) ------------> |
Handling the Chaos with Retransmission
What happens if a router gets overwhelmed and drops a packet?
TCP handles this using a timeout mechanism. When the server sends a packet, it starts a hidden stopwatch. If the client receives the packet, it sends an ACK. But if the packet is dropped in transit, the client never receives it, and therefore never sends the ACK.
Eventually, the server's stopwatch runs out. The server says, "I never got a receipt for packet #2. It must be lost." It then automatically retransmits packet #2.
Client Server
| |
| <------ Data Packet (Seq = 1) ----------------- |
| ------- ACK (Ack = 2) ------------------------> |
| |
| [ Packet Seq = 2 is LOST in transit ] |
| |
| (Server's timer expires...) |
| |
| <------ Data Packet (Seq = 2) [RETRANSMITTED] - |
| ------- ACK (Ack = 3) ------------------------> |
This mechanism is the heart of TCP's reliability. It is why you can download a 5GB video game over a spotty Wi-Fi connection and the file won't be corrupted. TCP is silently working in the background, keeping track of every byte and resending anything that fails.
Closing the Connection
When the data transfer is complete, TCP doesn't just hang up the phone. Connections consume memory and ports on both the client and the server. To free up those resources, TCP requires a formal tear-down process using the FIN (Finish) flag.
It is essentially a 4-way handshake, though the middle steps are often combined:
[ TCP Connection Termination ]
Client Server
| |
| ------- 1. FIN (I'm done sending) ------------> |
| <------ 2. ACK (Got it) ----------------------- |
| |
| <------ 3. FIN (I'm done too) ----------------- |
| ------- 4. ACK (Got it. Goodbye.) ------------> |
| |
v [ Connection Closed ] v
Both sides explicitly agree that the conversation is over. The ports are released, the memory is cleared, and the network is ready for the next task.
Why You Should Care
As a developer, you rarely have to write code that manually handles SYN flags or sequence numbers. The operating system handles TCP for you. You just write fetch('https://api.example.com') and let the browser do the rest.
But understanding TCP is what separates a junior developer from a senior engineer.
When your application is slow, is it your database, or is the network dropping packets and forcing TCP retransmissions? When a WebSocket disconnects, was it a clean FIN/ACK closure, or did the connection just abruptly drop? When you configure a cloud load balancer, why does it have an option for "TCP timeout"?
The internet is fundamentally unreliable. TCP is the brilliant, invisible engineering layer that makes it feel reliable. Once you understand the handshake and the receipts, the network stops being a black box.





