Skip to main content

Command Palette

Search for a command to run...

TCP Working: 3-Way Handshake & Reliable Communication

How TCP Keeps the Internet Reliable

Published
3 min read
A

Simple guy learning web development

Imagine trying to send a 1,000-page script to a publisher by mailing each page in a separate envelope. Now imagine the post office doesn't guarantee the order of delivery, or even that every envelope will arrive. Without a system to track those pages, the publisher might receive page 50 before page 1, or lose chapter three entirely.

The internet faces this exact problem every second. Data is broken into small "packets" that can take different paths to reach you. Without rules, your files would be corrupted, and your emails would make no sense. This is why we need TCP.

What is TCP and Why is it Needed?

TCP (Transmission Control Protocol) is the set of rules that ensures data gets from one device to another exactly as it was sent. It sits at the "Transport Layer" of the network.

It is designed to solve three major problems:

  1. Packet Loss: Data getting lost in transit.

  2. Out-of-Order Delivery: Packets taking different routes and arriving in the wrong sequence.

  3. Data Corruption: Bits of data being flipped or damaged during the journey.

The Lifecycle of a Connection

From the moment you click a link to the moment the page finishes loading, TCP has gone through an entire lifecycle:

  • Establish: Handshake (SYN → SYN-ACK → ACK)

  • Transfer: Sequencing, ACKs, and Retransmission.

  • Close: Graceful shutdown (FIN → ACK).

Step 1: The TCP 3-Way Handshake

Before TCP sends any actual data, it must establish a formal connection. It’s like a phone call, you don't start talking until you know the other person has picked up and said "Hello." This process is called the 3-Way Handshake.

How it Works:

  • Step 1: SYN (Synchronize)

    The Client sends a "SYN" message to the Server.

    Eg. "Hey, I want to talk to you. Here is my starting sequence number."

  • Step 2: SYN-ACK (Synchronize-Acknowledge)

    The Server receives the request and sends back a "SYN-ACK."

    Eg. "I hear you! I’m ready to talk too. I've received your number, and here is mine."

  • Step 3: ACK (Acknowledge)

    The Client receives the Server's response and sends a final "ACK."

    Eg. "Got it! Let’s start the data transfer."

Step 2: Data Transfer

Now that the connection is live, TCP begins moving data. It ensures reliability using Sequence Numbers and Acknowledgments (ACKs).

  • Ordering: Every packet is given a sequence number. If the receiver gets packet 2 before packet 1, TCP holds packet 2 in a waiting room until packet 1 arrives, then puts them in order.

  • Correctness: Each packet includes a "checksum"—a mathematical signature. If the packet arrives damaged, the receiver detects it and throws it away.

  • Handling Losses: If the sender doesn't receive an "ACK" for a specific packet within a certain timeframe, it assumes the packet was lost and sends it again automatically.

Step 3: Closing the Connection

When the data transfer is finished, TCP doesn't just hang up. It performs a graceful shutdown to ensure no data is left "in flight." This usually involves a four-step process using FIN (Finish) and ACK flags.

  1. Client: "I'm done sending data." (FIN)

  2. Server: "I hear you. I'm finishing my last tasks." (ACK)

  3. Server: "Okay, I'm done too. Close the connection." (FIN)

  4. Client: "Acknowledged. Goodbye!" (ACK)