# TCP Working: 3-Way Handshake & Reliable Communication

---

## Start With a Simple Question

**What happens if computers send data without rules?**

Imagine sending a long message in pieces:

* Some pieces arrive late
    
* Some arrive twice
    
* Some never arrive
    
* Some arrive in the wrong order
    

The receiver gets **confused data**.

This is exactly the problem the internet faced early on.

---

## What Is TCP and Why It Is Needed

**TCP (Transmission Control Protocol)** is a set of rules that makes sure data is:

* Delivered
    
* Delivered in order
    
* Delivered correctly
    

Simple definition:

> **TCP is a protocol that guarantees reliable communication between two computers.**

Without TCP, most internet applications would break.

---

## Problems TCP Is Designed to Solve

TCP exists to solve these real problems:

1. **Data loss** – packets may disappear
    
2. **Out-of-order delivery** – packets may arrive mixed
    
3. **Duplicate packets** – packets may arrive twice
    
4. **Network congestion** – network may be slow or overloaded
    
5. **Unreliable connections** – sender and receiver may not be ready
    

TCP handles all of this **automatically**.

---

## Big Picture: TCP Connection Lifecycle

![Image](https://www.ibm.com/support/pages/system/files/inline-images/Flow%20chart%20TCP%20connection_0.jpg align="left")

![Image](https://www.tcpipguide.com/free/diagrams/tcpclose.png align="left")

![Image](https://miro.medium.com/1%2AXwSot0zFwvX_8M8wIoJjIg.png align="left")

TCP has three main phases:

1. Connection setup
    
2. Data transfer
    
3. Connection close
    

---

## What Is the TCP 3-Way Handshake

Before sending data, TCP first asks:

> “Are you ready to talk reliably?”

This is done using the **3-Way Handshake**.

Purpose:

* Make sure both sides are reachable
    
* Agree to start a connection
    
* Synchronize communication
    

---

## 3-Way Handshake (Conversation Analogy)

Think of a phone call:

1. **Client**: “Can you hear me?”
    
2. **Server**: “Yes, I hear you. Can you hear me?”
    
3. **Client**: “Yes, let’s talk.”
    

Only after this does the conversation start.

---

## Step-by-Step: SYN, SYN-ACK, ACK

![Image](https://static.afteracademy.com/images/what-is-a-tcp-3-way-handshake-process-three-way-handshaking-establishing-connection-6a724e77ba96e241.jpg align="left")

![Image](https://www.flamingbytes.com/images/tcp-handshake-1.png align="left")

![Image](https://miro.medium.com/v2/resize%3Afit%3A1200/1%2AQSRdzOUUaloxzs84nl7exA.jpeg align="left")

### Step 1: SYN

Client → Server

> “I want to connect.”

This packet is called **SYN** (synchronize).

---

### Step 2: SYN-ACK

Server → Client

> “I received your request, and I’m ready.”

This packet is **SYN + ACK**.

---

### Step 3: ACK

Client → Server

> “I received your response.”

Connection is now **established**.

Important:

> No application data is sent before this completes.

---

## How Data Transfer Works in TCP

Once connected, TCP starts sending data in **small pieces** called segments.

Each piece has:

* A **sequence number**
    
* An expected **acknowledgement**
    

You don’t need exact numbers—just the idea.

---

## Sequence Numbers (High Level)

Sequence numbers answer:

> “Which part of the data is this?”

Example idea:

* Packet 1 → bytes 1–100
    
* Packet 2 → bytes 101–200
    

This allows the receiver to:

* Reorder packets
    
* Detect missing data
    

---

## Acknowledgements (ACKs)

After receiving data, the receiver sends back:

> “I got everything up to here.”

This ACK tells the sender:

* What arrived successfully
    
* What needs to be resent
    

---

## TCP Data Transfer Flow

![Image](https://static.wixstatic.com/media/a2409e_aef337e8e53946f383f37bad39fe1028~mv2.jpg/v1/fill/w_566%2Ch_480%2Cal_c%2Clg_1%2Cq_80/a2409e_aef337e8e53946f383f37bad39fe1028~mv2.jpg align="left")

![Image](https://www.firewall.cx/images/stories/tcp-analysis-section-2-5.gif align="left")

![Image](https://miro.medium.com/1%2AFwXZ_kk0uVwSfHhsoLA1uw.png align="left")

---

## How TCP Ensures Reliability, Order, and Correctness

### 1\. Reliability

* Lost packets are detected
    
* Missing data is resent
    

### 2\. Order

* Sequence numbers allow reordering
    
* Application receives data in correct order
    

### 3\. Correctness

* Checksums detect corrupted data
    
* Corrupted packets are discarded and resent
    

All of this happens **behind the scenes**.

---

## Packet Loss and Retransmission

What if a packet is lost?

![Image](https://www.researchgate.net/publication/382092781/figure/fig1/AS%3A11431281259508197%401720533616113/TCP-packet-loss-detection-and-retransmission-triggered-by-a-timeout-or-by-three-duplicate.ppm align="left")

![Image](https://assets.extrahop.com/images/infographics/TCP-RTO-Retransmission-Timeout-Diagram.jpg align="left")

![Image](https://book.systemsapproach.org/_images/f06-12-9780123850591.png align="left")

Flow:

1. Sender sends data
    
2. ACK is not received
    
3. Sender waits
    
4. Sender retransmits missing data
    

Application code never sees this complexity.

---

## How a TCP Connection Is Closed

Closing a TCP connection is also controlled.

TCP does **not** just disappear.

---

## Connection Termination (FIN and ACK)

![Image](https://www.tcpipguide.com/free/diagrams/tcpclose.png align="left")

![Image](https://www.excentis.com/web/image/122886-2cd58847/tekening_website_blog.webp?access_token=ecea94ef-81de-4cf4-93a0-58e365abdb55 align="left")

![Image](https://static.afteracademy.com/images/what-is-a-tcp-3-way-handshake-process-three-way-handshaking-establishing-connection-6a724e77ba96e241.jpg align="left")

Simple idea:

1. One side sends **FIN** → “I’m done sending”
    
2. Other side sends **ACK**
    
3. Other side sends **FIN**
    
4. First side sends **ACK**
    

Connection is now **cleanly closed**.

Why this matters:

* Prevents data loss
    
* Frees system resources safely
    

---

## Key Mental Model (Keep This)

* TCP is **careful**
    
* TCP checks everything
    
* TCP fixes problems automatically
    
* TCP trades speed for correctness
    

That’s why TCP is used for:

* HTTP / APIs
    
* Logins
    
* Payments
    
* File transfers
    

---

## Final Reassurance

You do **not** need to:

* Memorize packet formats
    
* Remember flags in detail
    
* Know exact numbers
    

If you understand:

> **Handshake → Reliable transfer → Clean close**

You understand TCP well enough to move forward.

---
