TCP Working: 3-Way Handshake & Reliable Communication
Imagine sending a 100-page document across the internet. The network breaks it into small packets — maybe 100 of them — and sends each one separately.
Here's the problem:
- Packet 47 takes a different route and arrives before packet 12
- Packet 83 gets lost somewhere in transit
- Packet 29 arrives corrupted
Without rules, the receiver gets a jumbled mess. Some pages missing, others in the wrong order, some unreadable.
This is why we need TCP.
What is TCP?
TCP (Transmission Control Protocol) is a set of rules that guarantees reliable communication between two computers. It ensures that data arrives complete, in order, and without errors.
When you load a webpage, send an email, or download a file, TCP is working behind the scenes to make sure nothing goes wrong.
TCP solves three fundamental problems:
- Lost data — Packets disappear in transit. TCP detects this and resends them.
- Out-of-order delivery — Packets arrive scrambled. TCP reassembles them correctly.
- Corrupted data — Bits get flipped during transmission. TCP detects and fixes errors.
Without TCP, the internet would be chaos. With TCP, it just works.
The Connection Problem
Before two computers can exchange data reliably, they need to agree on a few things:
- Are we both ready to communicate?
- What numbering system will we use to track packets?
- How will we confirm receipt?
This agreement happens through a process called the 3-way handshake.
The 3-Way Handshake
The handshake is a short conversation that establishes a TCP connection. Think of it like a phone call:
- You: "Hey, can you hear me?"
- Friend: "Yes, I can hear you. Can you hear me?"
- You: "Yes, I hear you too. Let's talk."
Only after this exchange do you start the actual conversation. TCP works the same way.
Step-by-Step: SYN, SYN-ACK, ACK
Let’s break down each step.
Step 1: SYN (Synchronize)
The client sends a SYN packet to the server. This packet says:
- “I want to establish a connection”
- “My starting sequence number is X”
The sequence number is a random starting point that will be used to track packets throughout the connection.
Client → Server "SYN: I want to connect. My sequence number starts at 1000."
Step 2: SYN-ACK (Synchronize + Acknowledge)
The server responds with a SYN-ACK packet. This packet says:
- “I received your request”
- “I agree to connect”
- “My starting sequence number is Y”
The server acknowledges the client’s sequence number and shares its own.
Server → Client "SYN-ACK: Got it. I acknowledge your 1000. My sequence number starts at 5000."
Step 3: ACK (Acknowledge)
The client sends a final ACK packet. This packet says:
- “I received your response”
- “We’re good to go”
Client → Server
"ACK: Got it. I acknowledge your 5000. Let's start."Connection established. Both sides now know:
- The other is ready
- What sequence numbers to expect
- Communication can begin
Why Three Steps?
Why not just two? Or one?
One step isn't enough: The server wouldn't know if the client received its response.
Two steps aren't enough: The client wouldn't confirm it received the server's information. The server would be guessing.
Three steps confirm both directions:
- Client confirms it can reach the server (SYN)
- Server confirms it can reach the client (SYN-ACK)
- Client confirms the round trip works (ACK)
This mutual confirmation is what makes TCP reliable from the very start.
How Data Transfer Works
Once the connection is established, data flows in segments. Each segment has:
- Sequence number — Position of this data in the stream
- Data — The actual payload
- Checksum — For error detection
How sequence numbers work:
- Client sends 500 bytes starting at sequence number 1001
- Server receives it and responds: "Got everything up to 1500. Send 1501 next."
- Client sends the next 500 bytes starting at 1501
- Server acknowledges: "Got everything up to 2000. Send 2001 next."
The acknowledgment number tells the sender what byte to send next. This is how TCP keeps everything in order.
How TCP Handles Packet Loss
What happens when a packet disappears?
The process:
- Client sends packet with sequence 1501
- Packet gets lost in transit
- Server never receives it, so it can't acknowledge
- Client waits for an ACK that never comes
- After a timeout, client assumes the packet was lost
- Client resends the same packet
- Server receives it this time and acknowledges
This retry mechanism ensures nothing is permanently lost. TCP will keep trying until the data gets through or the connection fails entirely.
How TCP Ensures Reliability
TCP uses several mechanisms working together:
1. Sequence Numbers
Every byte has a position. If something arrives out of order, TCP knows exactly where it belongs.
2. Acknowledgments
The receiver constantly tells the sender what it has received. No guessing.
3. Checksums
Each segment includes a checksum calculated from its contents. If the data is corrupted in transit, the checksum won't match, and TCP discards that segment.
4. Timeouts and Retransmission
If an acknowledgment doesn't arrive within a reasonable time, assume the packet was lost and send it again.
5. Flow Control
The receiver tells the sender how much data it can handle. This prevents overwhelming slow receivers.
Closing a TCP Connection
Connections don't last forever. When communication is complete, both sides need to close gracefully. This uses a 4-step process with FIN (finish) packets.
Why four steps?
Each side needs to:
- Signal it's done sending
- Acknowledge the other side is done
The connection is only fully closed when both sides have finished and acknowledged each other. This ensures no data is lost at the end of a conversation.
The Complete TCP Lifecycle
Every TCP connection follows this pattern:
- Establish — Handshake to set up the connection
- Transfer — Exchange data with reliability guarantees
- Close — Gracefully terminate when done
Quick Reference
Wrapping Up
TCP is a conversation with manners.
Before talking, both sides confirm they're ready (handshake). During the conversation, both sides confirm they heard each message (acknowledgments). When the conversation ends, both sides say goodbye properly (termination).
This politeness is what makes TCP reliable. Every packet is accounted for. Nothing slips through the cracks. And when something goes wrong, TCP notices and fixes it.
Most of the internet runs on TCP because most of the internet can't afford to lose data. Now you know exactly how it keeps that promise.
0 Comments
Sign in to join the conversation
No comments yet. Be the first to comment!