How TCP Works: The 3-Way Handshake and Reliable Communication

Imagine sending a 10-page letter by dropping each page into a different mailbox, with no page numbers and no way to know if any arrived. Some pages get lost. Some arrive in the wrong order. Some come twice. The reader gets a confusing mess.
That’s what sending data over a network looks like without rules. TCP is the set of rules that fixes it. This blog is for beginners, so we’ll keep it simple and skip packet-level details.
What is TCP and Why Do We Need It?
TCP (Transmission Control Protocol) is a protocol that makes sure data gets from one computer to another reliably, in order, and without damage.
Networks are messy. The internet is made of many devices passing little chunks of data (called packets) along different routes. Things go wrong, and TCP is the system that handles it, so your web pages, emails, and downloads arrive intact.
Problems TCP Solves
When data travels over a network, four things can go wrong:
Lost packets: some pieces never arrive.
Out-of-order packets: pieces arrive in a different order than they were sent.
Duplicate packets: the same piece arrives twice.
Corrupted packets: a piece arrives damaged.
TCP detects each of these and fixes them automatically, so the application on top (your browser, for example) never has to worry.
The 3-Way Handshake: Saying Hello Properly
Before sending any real data, TCP makes sure both sides are ready. It does this with a three-step greeting called the 3-way handshake.
Think of a phone call:
Step 1 (SYN): You call and say, “Hi, can you hear me?”
Step 2 (SYN-ACK): They answer, “Yes, I can hear you. Can you hear me?”
Step 3 (ACK): You say, “Yes, I can. Let’s talk.”
Now both sides know the line works in both directions.
In TCP terms:
SYN (synchronize): The client says, “I want to connect, and I’ll start counting my data from number X.”
SYN-ACK: The server says, “Got it. I’m ready too, and I’ll start counting from number Y.”
ACK (acknowledge): The client says, “Got yours too. Connection open.”
Those starting numbers are called sequence numbers, and they’re the key to everything that follows.
How Data Transfer Works
Once connected, TCP breaks your data into small pieces and gives each one a sequence number, like page numbers in a letter. The receiver sends back acknowledgements (ACKs) saying, “I got everything up to here.”
Sequence numbers let the receiver put pieces back in the right order, even if they arrive jumbled, and spot duplicates and throw them away.
How TCP Ensures Reliability
Packet loss and retransmission: What if a piece goes missing? The sender waits for an ACK. If it doesn’t arrive within a short time, the sender assumes the piece was lost and sends it again. This is called retransmission.
Correctness: Each piece also carries a small check value (a checksum). If the data arrives damaged, the receiver notices and discards it, and the missing ACK triggers a resend.
Flow control: TCP also stops a fast sender from overwhelming a slow receiver, so it adjusts its speed to what the other side can handle.
Together, these give us reliability (nothing lost), order (correct sequence), and correctness (nothing damaged).
How a TCP Connection is Closed
When both sides are done, they say goodbye politely, again using a conversation:
FIN: One side says, “I’m finished sending.”
ACK: The other side replies, “Got it.”
FIN: The other side says, “I’m finished too.”
ACK: The first side confirms, “Okay, bye.”
Closing takes four steps because each direction of the connection is closed separately.
Why This Matters for Developers
Almost everything you build on the web, including HTTP requests, APIs, and database connections, runs on top of TCP. When you see a “connection timeout” or “connection refused” error, the handshake is where things went wrong. And when a site feels slow, retransmissions caused by a bad network may be the reason.
Wrapping Up
TCP is a polite, careful conversation between two computers. They greet each other (3-way handshake), exchange numbered pieces and confirm each one (data transfer), resend anything lost (reliability), and say goodbye properly (connection close).
Comments
Join the discussionNo comments yet. Be the first to comment.
Networking Basics
How does the internet actually reach your code? This 6-part beginner series covers network devices, DNS, DNS records, TCP, TCP vs UDP, and hands-on testing with cURL.




