# TCP Working: 3-Way Handshake & Reliable Communication

## TL;DR

> **TCP (Transmission Control Protocol)** is a transport-layer protocol that ensures reliable data transfer. It starts communication with a **3-way handshake** to establish a connection, then guarantees data is delivered **in order, without loss, and with error correction** throughout the communication.
> 
> TCP establishes a connection using a **3-way handshake** (SYN → SYN-ACK → ACK), ensuring both the client and server are ready to communicate. Once the connection is set, TCP guarantees **reliable communication** by delivering data in order, detecting errors, retransmitting lost packets, and managing data flow and congestion. This ensures accurate, ordered, and complete data delivery, providing dependable communication over the internet.

---

Imagine sending data across a network with no rules, no confirmation, and no guarantee that anything arrives correctly. Packets could be lost, duplicated, or arrive in the wrong order, leaving applications confused and data incomplete. To prevent this chaos, the internet relies on structured communication protocols. **TCP addresses these problems by defining clear rules for connection setup and data transfer**, beginning with the **3-way handshake** and continuing with mechanisms that ensure reliable, ordered, and error-free communication between devices.

## TCP

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769523294357/baf2ca12-4cd7-4d04-bd0b-765e0d7b4d40.png align="center")

### **What is TCP?**

**<mark>Transmission Control Protocol </mark>** (TCP) is **a key, connection-oriented networking protocol that ensures reliable, ordered, and error-checked delivery of data between applications on an IP network**. Operating at the transport layer, it splits data into packets, manages their transmission, resends any lost data, and checks data integrity through a "three-way handshake."

**Key Characteristics of TCP:**

* **Connection-Oriented:** TCP requires a three-way handshake before starting data transmission to ensure a reliable connection.
    
* **Reliable:** TCP guarantees data delivery, maintains the correct packet order, and detects errors with retransmission if necessary.
    
* **Higher Overhead:** TCP uses larger headers (at least 20 bytes) and extra control mechanisms, which can lead to increased latency compared to UDP.
    
* **Flow & Congestion Control:** Adjusts data transmission rate to prevent overwhelming the receiver or the network.
    

### **Why it is needed?**

TCP is needed because the internet itself is unreliable—packets can be lost, duplicated, or arrive out of order. TCP solves these problems by establishing a connection, detecting errors, retransmitting lost data, and managing network congestion. This makes TCP essential for applications where **data accuracy and completeness matter**, such as web browsing (HTTP/HTTPS), email, and file transfers.

### Problems TCP is designed to solve

* **Packet Loss:**  
    Data packets can be lost during transmission; TCP detects this and retransmits missing packets.
    
* **Out-of-Order Delivery:**  
    Packets may arrive in the wrong sequence; TCP reorders them correctly before delivering data to the application.
    
* **Data Corruption:**  
    Packets can become corrupted; TCP uses error checking to detect and discard bad data.
    
* **Network Congestion:**  
    Too much data sent too quickly can overwhelm networks; TCP adjusts transmission speed to prevent congestion.
    
* **Flow Control:**  
    A fast sender may overwhelm a slow receiver; TCP manages data flow to match the receiver’s capacity.
    
* **Unreliable Underlying Network:**  
    Since IP does not guarantee delivery, TCP adds reliability on top of it.
    

> In short, TCP ensures **reliable, orderly, and efficient communication** over an otherwise unreliable network.

---

## TCP 3-Way Handshake

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769523988321/43d2b049-b28f-44ff-b068-297ede1ed17a.jpeg align="center")

The TCP 3-way handshake is a foundational process used to establish a reliable, connection-oriented session between a client and a server before data transfer. It involves three steps—SYN, SYN-ACK, and ACK—to synchronize sequence numbers and initialize connection parameters, ensuring both parties are ready for full-duplex communication. This process ensures both sides are ready to communicate and agree on initial settings.

**Steps of the TCP 3-Way Handshake**

1. **SYN (Synchronize)**: The client initiates the connection by sending a TCP segment with the SYN flag set to 111, a random initial sequence number (ISNcap I cap S cap N𝐼𝑆𝑁), and other parameters (e.g., maximum segment size).
    
2. **SYN-ACK (Synchronize-Acknowledgment)**: The server receives the SYN, sets the SYN and ACK flags to 111, and sends a response. This segment acknowledges the client's SYN (using the client's sequence number +1positive 1+1) and sends its own initial sequence number.
    
3. **ACK (Acknowledgment)**: The client acknowledges the server's SYN-ACK by sending a segment with the ACK flag set to 111. The sequence number is incremented, and the connection is officially established (ESTABLISHED state).
    

**Key Points to remember**

* **Purpose**: To synchronize sequence numbers, ensure both sides can communicate, and prevent old, delayed packets from interfering with a new connection.
    
* **Flags**: SYN (synchronize) and ACK (acknowledge) are critical TCP header flags.
    
* **Sequence Numbers**: Random numbers used to order packets and ensure reliable data delivery.
    

After this process, the client and server can begin exchanging data in both directions simultaneously (full-duplex).

---

## Data Transfer in TCP

The data transfer in TCP works in the following stages:

1. **Connection Established:**  
    Before any data is sent, TCP establishes a connection using the **3-way handshake**.
    
2. **Data Segmentation:**  
    The data is broken into smaller units called **segments**, each with a sequence number.
    
3. **Ordered Transmission:**  
    Each data segment is assigned a **sequence number**, allowing the receiver to **reassemble packets in the correct order**, even if they arrive out of sequence.
    
4. **Acknowledgments (ACKs):**  
    TCP uses **acknowledgments (ACKs)** to confirm successful delivery of data. If packets are lost, TCP detects the loss and **retransmits** them.
    
5. **Retransmission:**  
    If a data segment is **lost, delayed, or corrupted**, TCP detects the issue using **missing acknowledgments or checksum errors** and **retransmits the segment** to ensure complete and accurate delivery.
    
6. **Flow Control:**  
    TCP prevents overwhelming the receiver by adjusting the data transmission rate based on the receiver’s capacity.
    
7. **Congestion Control:**  
    TCP monitors network conditions and dynamically reduces or increases the sending rate to avoid congestion.
    

Together, these mechanisms ensure that data is delivered **accurately, in sequence, and without loss**, even over unreliable networks.

---

## Packet Loss and Retransmission

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769525816226/e10c6846-8283-4802-918d-5a21c8ff1327.png align="center")

In above diagram, the sender transmits data segments in order: **Segment 1, Segment 2, Segment 3, Segment 4, and Segment 5**.

* **Packet Loss Occurs:**  
    **Segment 2 is lost** in transit and never reaches the receiver.
    
* **Receiver Detects the Gap:**  
    The receiver successfully gets **Segment 1**, then receives **Segment 3, 4, and 5** out of order.  
    Since TCP expects data in sequence, the receiver **cannot move forward** without Segment 2.
    
* **Duplicate ACKs Sent:**  
    For each out-of-order segment received (Segments 3, 4, and 5), the receiver sends an **ACK repeatedly requesting Segment 2**.  
    These are called **duplicate ACKs**.
    
* **Fast Retransmit Triggered:**  
    When the sender receives **3 duplicate ACKs** for Segment 2, it assumes that Segment 2 was lost (not just delayed).
    
* **Retransmission:**  
    The sender **immediately retransmits Segment 2** without waiting for a timeout.
    

> **Why this matters?**  
> This mechanism, known as **<mark>Fast Retransmit</mark>**, allows TCP to quickly recover from packet loss, improving performance and maintaining **reliable and ordered data delivery**.
> 
> There are other algorithms other than **Fast Retransmit** for **Packet Loss & Retransmission like** Fast Recovery, RTO (Retransmission Time Out), RACK (Recent acknowledgment), Tail Loss Probe (TLP), Selective Acknowledgments (SACK).

---

## **TCP Connection Termination (TCP Teardown)**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769525243753/f055ad4c-0d50-4696-b478-47989e75803b.png align="center")

A TCP connection is closed **<mark>using a 4-way handshake process</mark>**, ensuring both sides (client and server) agree to terminate the connection and finish sending data. The initiator sends a `FIN` (Finish) packet, which is acknowledged (`ACK`) by the receiver, followed by a `FIN` from the receiver and a final `ACK`.

**4-Way Handshake Process:**

1. **FIN (Step 1):** The initiator (client or server) sends a `FIN` segment, indicating it has no more data to send, moving to the `FIN-WAIT-1` state.
    
2. **ACK (Step 2):** The receiver acknowledges the `FIN` with an `ACK` packet. The receiver may still send data, and the initiator is now in the `FIN-WAIT-2` state.
    
3. **FIN (Step 3):** Once the receiver is finished sending its own data, it sends its own `FIN` segment to the initiator.
    
4. **ACK (Step 4):** The initiator acknowledges the receiver's `FIN` with an `ACK` packet. The connection is now closed.
    

---

## Conclusion

TCP and the 3-way handshake are essential for reliable internet communication. TCP ensures data is delivered correctly and in order, even on unreliable networks. The 3-way handshake establishes a trusted connection before data exchange, ensuring both sides are ready. Together, they enable dependable data transfer for applications needing accuracy. Understanding TCP is crucial for networking basics, troubleshooting, and designing reliable systems.

---

> If you enjoyed this article, check out my other blogs on this profile.
> 
> 🔗 **Connect with me:**  
> [**LinkedIn**](https://linkedin.com/in/rajharsh4618) | [**GitHub**](https://github.com/Harsh-Raj4618) | [**X (Twitter)**](https://x.com/Harsh_Raj4618)
