# TCP Working: 3-Way Handshake & Reliable Communication

Have you ever wondered how your messages reach exactly the right person when you're chatting online? Or how Netflix knows which part of a movie to send to your screen next?

The answer is **TCP** – one of the most important protocols running quietly in the background every time you use the internet.

And honestly? When I first learned about it, I thought, "Wait, there's this much going on just to send a simple message?"

Let me break it down for you in the simplest way possible.

---

## What Happens If We Send Data Without Any Rules?

Imagine you're trying to tell a story to your friend over a bad phone connection.

You start talking:

* "So yesterday I went to…"
    
* *Connection drops*
    
* "…and then I saw a dog…"
    
* *Static noise*
    
* "…it was hilarious!"
    

Your friend only hears **random pieces** of your story.

They don't know:

* What came first
    
* What's missing
    
* If they heard everything
    

That's exactly what happens when computers try to send data **without rules**.

Packets (chunks of data) can:

* Arrive in the **wrong order**
    
* Get **lost** along the way
    
* Arrive **corrupted** or damaged
    
* Arrive **multiple times** (duplicates)
    

A mess, right?

This is where **TCP** steps in to fix everything.

---

## What is TCP and Why Do We Need It?

**TCP** stands for **Transmission Control Protocol**.

Think of it as a **rulebook** that ensures:

* Data reaches the destination **safely**
    
* Data arrives in the **correct order**
    
* Nothing is **lost or duplicated**
    
* Both sides **agree** before starting communication
    

It's like having a professional delivery service instead of just throwing your package out the window and hoping it lands at the right house.

### Where is TCP used?

Pretty much everywhere:

* Web browsing (HTTP/HTTPS)
    
* Email (SMTP, IMAP)
    
* File transfers (FTP)
    
* Video calls and streaming
    

Basically, **any application that can't afford to lose data** uses TCP.

---

## Problems TCP is Designed to Solve

Let me make this super clear.

Without TCP, here's what could go wrong:

### 1\. **Packets Arrive Out of Order**

Imagine receiving a book where:

* Chapter 5 comes first
    
* Then Chapter 2
    
* Then Chapter 8
    

You'd have no idea what the story is about.

**TCP fixes this** by numbering each piece of data so it can be reassembled correctly.

---

### 2\. **Packets Get Lost**

Sometimes data just disappears on the network.

Like sending 10 letters in the mail, but only 7 arrive.

**TCP notices the missing pieces** and asks for them again.

---

### 3\. **Packets Arrive Corrupted**

Data can get damaged during transmission.

Think of it like a letter that got soaked in the rain and half the words are unreadable.

**TCP checks each packet** to make sure it's not damaged. If it is, TCP throws it away and requests a fresh copy.

---

### 4\. **No Confirmation of Delivery**

If you send a message and never get a reply, you don't know if it was received.

**TCP makes sure both sides confirm** they got the data.

It's like:

* You: "Did you get my message?"
    
* Friend: "Yes, I got it!"
    

Now you're sure.

---

## What is the TCP 3-Way Handshake?

Before two computers start sending actual data, they need to **introduce themselves** and **agree to communicate**.

This process is called the **3-way handshake**.

Think of it like this:

**You want to call a friend:**

1. **You:** "Hey, can you hear me?" *(SYN)*
    
2. **Friend:** "Yes, I can hear you! Can you hear me?" *(SYN-ACK)*
    
3. **You:** "Yes, I can hear you too! Let's talk." *(ACK)*
    

Now the conversation begins.

That's exactly how TCP works.

Let me show you step by step.

---

## Step-by-Step: The TCP 3-Way Handshake

### Step 1: Client Sends SYN (Synchronize)

The **client** (your computer) wants to connect to a **server** (like Google or Netflix).

It sends a message:

> "Hey server, I want to talk to you. Here's my starting sequence number: **1000**."

This message is called **SYN** (Synchronize).

The sequence number is like a **ticket number** – it helps keep track of the data being sent.

---

### Step 2: Server Sends SYN-ACK (Synchronize-Acknowledge)

The server receives the SYN message and replies:

> "Got it! I'm ready to talk. Here's my starting sequence number: **5000**. And yes, I received your number **1000**."

This message is called **SYN-ACK**.

It's doing **two things**:

1. Sending its own sequence number (**SYN**)
    
2. Acknowledging the client's number (**ACK**)
    

---

### Step 3: Client Sends ACK (Acknowledge)

The client receives the server's reply and sends back:

> "Perfect! I got your number **5000**. Let's start!"

This is the final **ACK** (Acknowledgement).

Now both sides are **synchronized** and ready to exchange data.

---

### Diagram: TCP 3-Way Handshake

```plaintext
Client                          Server
  |                                |
  |-------- SYN (seq=1000) ------->|
  |                                |
  |<----- SYN-ACK (seq=5000) ------|
  |       (ack=1001)               |
  |                                |
  |-------- ACK (ack=5001) ------->|
  |                                |
  |   Connection Established!      |
```

Notice how the acknowledgment numbers are **one more** than the sequence numbers?

That's TCP saying: "I got your number, and I'm ready for the next one."

---

## How Data Transfer Works in TCP

Once the handshake is done, data transfer begins.

Here's how it works:

### 1\. Data is Split into Segments

Your data (like a webpage or video) is too big to send all at once.

So TCP **breaks it into small pieces** called **segments**.

Each segment gets a **sequence number**.

---

### 2\. Segments are Sent One by One

The client sends:

```plaintext
Segment 1 (seq=1000, data="Hello")
Segment 2 (seq=1005, data="World")
```

---

### 3\. Server Acknowledges Each Segment

After receiving each piece, the server replies:

```plaintext
ACK (ack=1005) → "Got segment 1"
ACK (ack=1010) → "Got segment 2"
```

This way, the client **knows for sure** that the data was received.

---

### Diagram: Data Transfer with Sequence Numbers

```plaintext
Client                          Server
  |                                |
  |--- seq=1000 ("Hello") -------->|
  |                                |
  |<------- ack=1005 --------------|
  |                                |
  |--- seq=1005 ("World") -------->|
  |                                |
  |<------- ack=1010 --------------|
```

Simple, right?

---

## How TCP Ensures Reliability, Order, and Correctness

Now let's talk about the **magic** of TCP.

### 1\. **Ordering Data Using Sequence Numbers**

Even if packets arrive out of order, TCP uses sequence numbers to **reassemble them correctly**.

Example:

* Packet 3 arrives first
    
* Then Packet 1
    
* Then Packet 2
    

TCP **reorders them** before passing the data to your application.

---

### 2\. **Detecting Lost Packets**

If the client sends segment 1000 but never gets an ACK back, it knows something went wrong.

After waiting a bit, TCP **resends** the segment.

This is called **retransmission**.

---

### 3\. **Checking for Corruption**

Every TCP segment includes a **checksum** – a small number calculated from the data.

When the server receives the segment, it recalculates the checksum.

If the numbers **don't match**, the data is **corrupted** and is thrown away.

The client will resend it automatically.

---

### Diagram: Packet Loss and Retransmission

```plaintext
Client                          Server
  |                                |
  |--- seq=1000 ("Hello") -------->|
  |                                |
  |         (packet lost)          |
  |                                |
  |   ... timeout ...              |
  |                                |
  |--- seq=1000 ("Hello") -------->|  (resend)
  |                                |
  |<------- ack=1005 --------------|
```

TCP is patient. It waits, and if needed, tries again.

---

## How a TCP Connection is Closed

Once the data transfer is complete, the connection needs to be **closed properly**.

This is done using a **4-step process** with **FIN** (Finish) and **ACK** messages.

### Step 1: Client Sends FIN

The client says:

> "I'm done sending data."

---

### Step 2: Server Sends ACK

The server replies:

> "Got it. I'll finish up."

---

### Step 3: Server Sends FIN

The server says:

> "I'm done too."

---

### Step 4: Client Sends ACK

The client replies:

> "Okay, goodbye!"

Now the connection is **fully closed**.

---

### Diagram: TCP Connection Termination

```plaintext
Client                          Server
  |                                |
  |---------- FIN ---------------->|
  |                                |
  |<---------- ACK ----------------|
  |                                |
  |<---------- FIN ----------------|
  |                                |
  |---------- ACK ---------------->|
  |                                |
  |   Connection Closed!           |
```

It's polite, right? Both sides agree before hanging up.

---

## TCP Connection Lifecycle (Full Picture)

Let me show you the **entire journey** of a TCP connection:

```plaintext
1. Establish Connection (3-Way Handshake)
   ↓
2. Transfer Data (with sequence numbers and ACKs)
   ↓
3. Close Connection (FIN and ACK)
```

Every time you load a website or stream a video, this entire cycle happens in **milliseconds**.

Pretty amazing.

---

## Why TCP is So Important

Honestly, without TCP, the internet as we know it wouldn't exist.

Here's why:

* **Web browsing** wouldn't work reliably
    
* **Emails** could arrive incomplete
    
* **File downloads** would get corrupted
    
* **Video calls** would be a disaster
    

TCP is the **invisible hero** making sure everything just works.

---

## About the Author

Hi, I'm **Saurabh Prajapati** – a full-stack software engineer at **IBM India Software Lab**, where I work on building cloud-native enterprise solutions with **Maximo**.

I specialize in **GenAI, React, and modern web technologies**, and I love breaking down complex topics like networking, APIs, and software architecture into simple, beginner-friendly explanations.

If you found this helpful, feel free to connect with me:

* **GitHub:** [prajapatisaurabh](https://github.com/prajapatisaurabh)
    
* **LinkedIn:** [saurabh-prajapati](https://linkedin.com/in/saurabh-prajapati)
    
* **Email:** saurabhprajapati120@gmail.com
    

Let's learn and build together!
