Skip to main content

Command Palette

Search for a command to run...

TCP Working: 3-Way Handshake & Reliable Communication

Updated
3 min read

Imagine sending a 1,000-piece jigsaw puzzle through the mail, but instead of a box, you send each piece in its own tiny envelope. Without rules, some pieces might arrive late, some might get lost, and the receiver would have no idea how to put them together.

In the digital world, TCP (Transmission Control Protocol) is the set of rules that solves this. It ensures that data isn't just sent, but is received completely, in order, and without errors.

Why We Need TCP: Solving the Chaos

If we sent data without a protocol like TCP, we would face three major problems:

  1. Lost Data: A packet might disappear in transit, leaving you with a broken file.

  2. Out-of-Order Delivery: Packet #5 might arrive before Packet #1, making a mess of the information.

  3. Corruption: Data might get "flipped" or damaged during the journey.

TCP is designed to solve these by creating a stable "virtual circuit" between two computers.

The TCP 3-Way Handshake: The "Digital Greeting"

Before TCP sends a single byte of real data (like a photo or a message), it must establish a connection. It does this through a process called the 3-Way Handshake.

Think of it like a formal phone conversation:

  1. Step 1: SYN (Synchronize)

    • Computer A says: "Hey, I want to talk to you. Here is my starting sequence number so we can keep track of our messages."
  2. Step 2: SYN-ACK (Synchronize-Acknowledgment)

    • Computer B says: "I hear you! I'm ready to talk too. I received your request, and here is my starting sequence number so you can track my messages."
  3. Step 3: ACK (Acknowledgment)

    • Computer A says: "Got it! We are both ready. Let's start sending data."

Once this "handshake" is complete, the connection is established, and the actual data transfer begins.

How TCP Ensures Reliability

Once the conversation starts, TCP uses a system of Sequence Numbers and Acknowledgments to stay organized.

  • Ordering: Every packet is numbered (e.g., 1, 2, 3). If they arrive as 1, 3, 2, TCP waits and reorders them correctly before showing them to the application.

  • Reliability (Retransmission): If Computer A sends Packet #2 but never receives an "Acknowledgment" (ACK) back from Computer B, it assumes the packet was lost and sends it again.

  • Correctness: TCP uses a "checksum" to verify the data hasn't been corrupted. If the math doesn't add up, it asks for a fresh copy.

Closing the Connection: The 4-Way Terminate

When the data transfer is finished, TCP doesn't just "hang up." It follows a polite closing procedure using FIN (Finish) and ACK flags.

  1. A sends a FIN: "I'm done sending data."

  2. B sends an ACK: "I hear you, I'm just finishing up my last few tasks."

  3. B sends a FIN: "Okay, I'm done now too."

  4. A sends an ACK: "Goodbye!"

This ensures that both sides have finished their work and no data is cut off prematurely.

Summary

  • Handshake: Establishes trust and synchronizes numbers.

  • Sequencing: Keeps data in the right order.

  • Retransmission: Fixes lost data by sending it again.

  • Termination: Closes the connection gracefully.

TCP is the backbone of the web because it turns an unreliable internet into a reliable stream of information.