Skip to main content

Command Palette

Search for a command to run...

TCP working: 3-way handshake & reliable communication

Updated
4 min read
TCP working: 3-way handshake & reliable communication

The internet is essentially that noisy room. Millions of devices are shouting data at once, and cables can get overloaded, causing pieces of information to just... vanish. If we sent data without rules, our emails would arrive scrambled, and our software updates would be missing vital pieces of code, rendering them useless.

This is why we have TCP (Transmission Control Protocol). It is the "polite conversation" protocol that ensures every single bit of data arrives exactly as intended.

The Problems TCP Solves

Before we look at how it works, we have to understand the chaos it prevents. When data travels across the web, it is broken into small "packets." Without TCP, three big things go wrong:

  1. Packet Loss: A packet gets dropped by a router and never arrives.

  2. Out-of-Order Delivery: Packet #3 arrives before Packet #1 because it took a different physical path.

  3. Corruption: A packet arrives, but its data was mangled during the journey.

TCP is designed to be the "quality control manager" that fixes all of these issues.

The Secret Sauce: The 3-Way Handshake

Before TCP sends a single byte of actual data (like your profile picture or a chat message), it needs to make sure the other side is ready, willing, and able to talk. It does this through a process called the 3-Way Handshake.

Imagine two people, Alice and Bob, trying to start a phone call.

Step 1: SYN (Synchronize)

Alice sends a message: "Hey Bob, I want to talk. My starting sequence number is 1000." In technical terms, the client sends a SYN packet. This packet contains a random "Sequence Number" that will be used to keep the data in order later.

Step 2: SYN-ACK (Synchronize-Acknowledge)

Bob hears Alice and responds: "I hear you, Alice! I’m ready too. I received your 1000, so I’m looking for 1001 next. Also, my starting sequence number is 5000." The server sends a SYN-ACK. It acknowledges Alice’s request and sends its own sequence number.

Step 3: ACK (Acknowledge)

Alice finishes the handshake: "Got it, Bob! I’m looking for your 5001 next. Let’s start!" Alice sends an ACK packet back. The "connection" is now officially established.

TCP protocol works

How Data Actually Moves

Once the handshake is done, the data transfer begins. But TCP doesn't just "send and forget." It uses a system of Sequence Numbers and Acknowledgements.

Think of it like a teacher handing out numbered worksheets to a student. The teacher gives the student Page 1, Page 2, and Page 3. The student then shouts back, "I have up to Page 3!"

If the teacher sends Page 4, but the student never acknowledges receiving it, the teacher assumes it was lost and sends Page 4 again. This is called Retransmission.

Reliability, Order, and Correctness

  • Reliability: If the sender doesn't get an "ACK" for a specific packet within a certain timeframe, it sends that packet again.

  • Order: If packets arrive in the order 1, 3, 2, TCP looks at the sequence numbers and puts them back in the correct order (1, 2, 3) before showing them to the user.

  • Correctness: Every TCP packet has a "checksum"—a digital fingerprint. If the fingerprint doesn't match the data, TCP rejects it as corrupted and asks for a fresh copy.

Saying Goodbye: Closing the Connection

A TCP connection isn't just "cut off" when the data is done; it is politely closed. This is often called a "Four-Way Teardown" because both sides have to agree to stop.

  1. FIN: The client sends a message saying, "I'm done sending data."

  2. ACK: The server says, "I hear you, I'll stop expecting data from you." (The server can still send data for a moment if it needs to finish something up).

  3. FIN: The server then sends its own message: "I'm done too."

  4. ACK: The client sends a final acknowledgement: "Understood. Goodbye!"

Summary

TCP is the reason the modern internet feels so stable. It takes a chaotic, unreliable network and turns it into a virtual "circuit" where data is guaranteed to be perfect. While the overhead of handshakes and acknowledgements makes it slightly slower than UDP, that is a small price to pay for knowing your data will arrive in one piece.