TCP Working: 3-Way Handshake & Reliable Communication

Imagine you're trying to talk to someone, but there are no rules. You might:
Send messages that never arrive
Get messages in the wrong order
Get the same message twice
Get messages that are broken or incomplete
Never know if your message was received
This is what happens when data is sent over the internet without proper rules. Without rules, the internet would be a mess. Files would come broken, websites wouldn't load right, and programs would crash all the time.
This is where TCP comes in—it's a set of rules that makes internet communication reliable and trustworthy.
What is TCP and Why It is Needed
TCP stands for Transmission Control Protocol. It's a set of rules that makes sure data gets from one computer to another safely, in the right order, and without errors.
Think of TCP like a reliable mail service. Instead of just throwing letters in the mail and hoping they arrive, TCP:
Makes sure all data arrives completely
Keeps data in the right order
Finds and fixes errors
Confirms that data was received
TCP is needed because the basic internet rules (called IP) only send data from one address to another—they don't make sure it arrives, arrives in order, or arrives correctly. TCP works on top of IP to fix these problems, making the internet reliable enough for:
Browsing websites
Sending emails
Downloading files
Watching videos
Online banking
Pretty much everything that needs reliable internet
Without TCP, the internet as we know it wouldn't work.
Problems TCP is Designed to Solve
TCP fixes several big problems that happen when sending data over the internet:
1. Data Loss
Data can get lost while traveling through the internet. Maybe the network is too busy, or something breaks. TCP finds lost data and sends it again automatically.
2. Wrong Order
Data can take different paths through the internet and arrive in the wrong order. TCP uses numbers to put everything back in the right order.
3. Duplicate Data
The same data might arrive twice. TCP finds these duplicates and throws away the extra ones.
4. Broken Data
Data can get damaged while traveling. TCP checks if data is broken and asks for it to be sent again.
5. Too Much Data at Once
If one computer sends data too fast, it can overwhelm the other computer. TCP controls how fast data is sent so the receiving computer can handle it.
6. Connection Setup
TCP makes sure both computers are ready to talk before sending data, and properly closes the connection when done.
What is the TCP 3-Way Handshake
Before two computers can send data to each other safely, they need to connect first. This is called the 3-way handshake.
Think of it like a phone call:
You (Your Computer): "Hello, are you there? I want to talk."
Them (Server): "Yes, I'm here! I heard you, and I'm ready to talk too."
You (Your Computer): "Great! I heard you back. Let's start talking!"

Just like this conversation, the 3-way handshake makes sure both computers can send and receive messages before any real data is sent. It's called "3-way" because it takes exactly three messages to finish.
Why Three Messages?
TCP works both ways—both computers need to send and receive data. The handshake makes sure:
Your computer can send to the server
The server can receive from your computer
The server can send to your computer
Your computer can receive from the server
This needs three confirmations to make a reliable connection that works both ways.
Step-by-Step Working of SYN, SYN-ACK, and ACK
Let's look at each step of the 3-way handshake:
Step 1: SYN (Synchronize)
Your computer starts the connection by sending a SYN message.
Your Computer Server
| |
|-------- SYN (Number: 1000) --->|
| |
What happens:
Your computer sends a SYN message to the server
This message includes your computer's starting number (let's say 1000)
The number is like a starting point for counting
The message says: "I want to connect, and I'm starting my count at 1000"
What the server thinks: "I got a connection request. They want to start at number 1000."
Step 2: SYN-ACK (Synchronize-Acknowledge)
The server replies with a SYN-ACK message.
Your Computer Server
| |
|<---- SYN-ACK (Number: 5000, ---|
| Got: 1001) |
What happens:
The server sends a SYN-ACK message back
This message has:
The server's starting number (let's say 5000)
A confirmation number of 1001 (your number + 1)
The confirmation number says: "I got your number 1000, and I'm ready for 1001"
The message says: "I got your message, I'm ready too, and I'm starting my count at 5000"
What your computer thinks: "The server got my message and is ready. They're starting at number 5000."
Step 3: ACK (Acknowledge)
Your computer sends one final ACK message.
Your Computer Server
| |
|-------- ACK (Got: 5001) ------>|
| |
What happens:
Your computer sends an ACK message to the server
This message has a confirmation number of 5001 (server's number + 1)
This says: "I got your number 5000, and I'm ready for 5001"
The message says: "Perfect! I got your reply. We're connected!"
What the server thinks: "The computer confirmed my message. We're connected!"
Complete 3-Way Handshake Flow
Your Computer Server
| |
|-------- SYN (Number: 1000) --->|
| |
|<---- SYN-ACK (Number: 5000, ---|
| Got: 1001) |
| |
|-------- ACK (Got: 5001) ------>|
| |
| CONNECTION READY |
| |
Result: The connection is now ready, and both computers can safely send data to each other.
How Data Transfer Works in TCP
Once the connection is ready, data flows both ways using sequence numbers and acknowledgment numbers.
Sequence Numbers
Every piece of data sent through TCP gets a number. These numbers help:
Know where each piece of data belongs
Find missing or out-of-order data
Ask for lost data to be sent again
Acknowledgment Numbers
When data is received, the receiving computer sends back a confirmation (ACK) that says:
Which data was received successfully
What number to send next
Data Transfer Flow
Your Computer Server
| |
|-- Data (Numbers: 1001-1100) --->|
| |
|<-------- ACK (Got: 1101) ------|
| |
|-- Data (Numbers: 1101-1200) --->|
| |
|<-------- ACK (Got: 1201) ------|
| |
Step-by-step example:
Your computer sends data:
Sends 100 pieces of data
Numbers: 1001 to 1100
Server gets all 100 pieces
Server confirms:
Server sends ACK with number 1101
This means: "I got everything up to 1100, send me 1101 next"
Your computer sends more data:
Sends next 100 pieces
Numbers: 1101 to 1200
Server gets all 100 pieces
Server confirms again:
Server sends ACK with number 1201
This means: "I got everything up to 1200, send me 1201 next"
This keeps happening during the data transfer, so both sides know exactly what data arrived safely.
Two-Way Communication
TCP lets data flow both ways at the same time:
Your Computer Server
| |
|-- Data (Numbers: 1001-1100) --->|
|<-- Data (Numbers: 5001-5100) ---|
| |
|-------- ACK (Got: 5101) ------->|
|<-------- ACK (Got: 1101) -------|
| |
Both your computer and the server can send data and confirmations at the same time, making TCP efficient for two-way communication.
How TCP Ensures Reliability, Order, and Correctness
TCP uses three main ways to make sure data arrives safely, in order, and correctly:
1. Reliability Through Confirmations and Resending
TCP makes sure no data is lost by using confirmations and automatically resending lost data:
How it works:
The sender keeps track of what data was sent
The receiver sends confirmations for received data
If a confirmation doesn't come back in time, TCP thinks the data was lost
The sender automatically sends the data again
This keeps happening until the sender gets confirmation
Data Loss and Resending Flow:
Your Computer Server
| |
|-- Data (Numbers: 1001-1100) --->|
| [Data Lost!] |
| |
| [Waiting...] |
| |
|-- Data (Numbers: 1001-1100) --->| (Sending again)
| |
|<-------- ACK (Got: 1101) ------|
| |
What happened:
Your computer sends data (numbers 1001-1100)
Data gets lost in the internet
Your computer waits for confirmation but doesn't get it
After waiting, your computer thinks the data was lost
Your computer sends the same data again
Server gets it and sends confirmation
Everything continues normally
2. Order Through Sequence Numbers
TCP makes sure data arrives in the right order using sequence numbers:
How it works:
Every piece of data gets a number
Data may arrive out of order because it takes different paths
TCP holds onto out-of-order data
Data is only given to the program when all previous data has arrived
This makes sure the program gets data in the right order
Out-of-Order Delivery Example:
Your computer sends:
Data 1: Numbers 1001-1100 (arrives first) ✓
Data 2: Numbers 1201-1300 (arrives second, wrong order!)
Data 3: Numbers 1101-1200 (arrives third)
Server's TCP:
- Gets Data 1 → holds it, gives it to the program
- Gets Data 2 → holds it (waiting for Data 3)
- Gets Data 3 → now has everything, gives it in order:
Data 1 (1001-1100)
Data 3 (1101-1200)
Data 2 (1201-1300)
3. Correctness Through Error Checking
TCP finds broken data using error checking codes:
How it works:
Every TCP message includes an error checking code
The sender calculates this code based on the data
The receiver calculates the code again and compares it
If the codes don't match, the data was broken
The broken data is thrown away
The sender will send it again when no confirmation is received
Error Checking Example:
Your Computer Server
| |
|-- Data + Error Code ---------->|
| [Data gets broken] |
| |
| [Error check fails] |
| [Data thrown away] |
| |
| [No confirmation sent] |
| [Waiting...] |
| |
|-- Data + Error Code ---------->| (Sending again)
| |
|<-------- ACK ------------------|
| |
How a TCP Connection is Closed
Just like TCP carefully starts a connection, it also carefully closes it. The closing process uses FIN (Finish) and ACK (Acknowledge) messages.
Why Close Properly?
A connection must be closed properly to make sure:
All data is sent and received before closing
No data is lost when closing
Both computers know the connection is ending
Everything is cleaned up properly
The 4-Way Handshake (Connection Closing)
Unlike the 3-way handshake for starting a connection, closing a connection usually needs 4 messages because each direction (your computer→server and server→your computer) is closed separately.
Step 1: Your Computer Sends FIN
Your Computer Server
| |
|-------- FIN ------------------>|
| |
Your computer sends a FIN message
This means: "I'm done sending data"
Your computer can still receive data from the server
Step 2: Server Sends ACK
Your Computer Server
| |
|<-------- ACK ------------------|
| |
The server confirms your computer's FIN
This means: "I got your close message"
The server can still send remaining data to your computer
Step 3: Server Sends FIN
Your Computer Server
| |
|<-------- FIN ------------------|
| |
When the server is ready to close, it sends its own FIN message
This means: "I'm also done sending data"
Step 4: Your Computer Sends ACK
Your Computer Server
| |
|-------- ACK ------------------>|
| |
Your computer confirms the server's FIN
Your computer waits a bit to handle any delayed messages
The server gets the ACK and closes the connection
After waiting, your computer also closes the connection
Complete Connection Closing Flow
Your Computer Server
| |
|-------- FIN ------------------>|
| |
|<-------- ACK ------------------|
| |
|<-------- FIN ------------------|
| |
|-------- ACK ------------------>|
| |
| CONNECTION CLOSED |
| |
TCP Connection Lifecycle
READY
|
| (3-way handshake)
|
LISTENING ←→ SENDING SYN
| |
| |
| GOT SYN
| |
| |
| READY ←→ READY
| | |
| | (Sending Data)
| | |
| | |
| WAITING ←→ CLOSING
| | |
| | |
| WAITING ←→ LAST ACK
| | |
| | |
| WAITING ←→ CLOSED
| |
| |
| CLOSED
A Simple Code Example
Here's what a TCP connection looks like in code:
import socket
# Your computer side
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# This does the 3-way handshake automatically
client_socket.connect(('example.com', 80))
# Send data (TCP handles numbers and confirmations automatically)
client_socket.send(b'GET / HTTP/1.1\r\nHost: example.com\r\n\r\n')
# Receive data (TCP makes sure data arrives in order and safely)
response = client_socket.recv(4096)
# Close connection (does the FIN/ACK closing automatically)
client_socket.close()
What happens behind the scenes:
connect()→ Does the 3-way handshake (SYN, SYN-ACK, ACK)send()→ TCP adds numbers, handles confirmationsrecv()→ TCP makes sure data arrives in order, asks for resending if neededclose()→ Does the proper closing (FIN, ACK, FIN, ACK)
Key Takeaways
TCP turns unreliable internet communication into reliable data delivery by adding numbers, confirmations, and error checking
The 3-way handshake makes a confirmed connection before any real data is sent
Sequence numbers make sure data arrives in the right order
Confirmations and resending make sure no data is lost
Error checking codes find and fix broken data
Proper closing with FIN/ACK makes sure both sides close the connection cleanly
TCP works invisibly—programs get a simple, reliable data stream while TCP handles all the hard work
TCP is the foundation of reliable internet communication. Every time you visit a website, send an email, or watch a video, TCP is working behind the scenes to make sure your data arrives safely, in order, and without errors.




