# TCP 3-Way Handshake Process

The TCP 3-Way Handshake is a process used by the Transmission Control Protocol (TCP) to establish a reliable connection between a client and a server before data transfer. It ensures that both sides are synchronized and ready to communicate.

---

## Problems TCP is designed to solve

Without TCP, applications would need to handle these problems themselves:

* Packet loss.
    
* Out-of-order delivery.
    
* Data Corruption.
    
* Network Congestion
    
* Flow control.
    

---

## **TCP 3-way Handshake Process**

Communication over the internet follows the TCP/IP model. Applications like web browsers use the Application Layer, and their data is passed to the Transport Layer, where TCP and UDP work.

* TCP is widely used because it provides reliable communication.
    
* UDP is faster but unreliable (e.g., used in DNS lookups).
    

![TCP 3 Way Handshaking](https://media.geeksforgeeks.org/wp-content/uploads/handshake-1.png align="center")

* **Step 1 (SYN):** In the first step, the client wants to establish a connection with a server, so it sends a segment with SYN(Synchronize Sequence Number) which informs the server that the client is likely to start communication and with what sequence number it starts segments with
    
* **Step 2 (SYN + ACK):** Server responds to the client request with SYN-ACK signal bits set. Acknowledgement(ACK) signifies the response of the segment it received and SYN signifies with what sequence number it is likely to start the segments with
    
* **Step 3 (ACK):** In the final part client acknowledges the response of the server and they both establish a reliable connection with which they will start the actual data transfer.
    

---

## How data transfer works in TCP

### **Segmenting**

* When an application sends data (like an email or file), TCP breaks the data into smaller chunks called segments.
    
* Each segment has a header containing information like sequence numbers, ports, and flags.
    
* This makes it easier to send large amounts of data over the network reliably.
    

### **Routing via IP**

* Once TCP creates segments, they are handed to IP (Internet Protocol).
    
* IP is responsible for delivering the segments from the sender to the receiver, possibly through multiple routers.
    
* TCP doesn’t care about the path—IP handles routing and addressing.
    

### **Reassembly at Receiver**

* Segments may arrive out of order because they can take different paths through the network.
    
* TCP at the receiver uses sequence numbers to reassemble the segments into the correct order to reconstruct the original message.
    

### **Retransmission**

* If the sender does not receive an acknowledgment within a certain time, it resends the missing segment.
    
* This ensures no data is lost, making TCP reliable.
    

and lot more..

---

## How TCP Ensures Reliability, Order, and Correctness

* Retransmission  
    TCP **retransmits** the missing segment if an ACK is not received in time
    
* Order (Sequence Numbers)  
    Every byte has a sequence number. Receiver get Buffers out-of-order segments and reassembles data correctly.
    

---

## How a TCP Connection Is Closed

TCP connection termination is the process of closing an established TCP connection between two devices in an orderly way. It uses a four-step handshake (FIN-ACK exchange) to ensure that both sides have finished sending and receiving all data before the connection is fully closed.

## **Types of TCP Connection Release**

TCP supports two types of connection releases like most connection-oriented transport protocols: 

1. **Abrupt connection release:** In an Abrupt connection release, either one TCP entity is forced to close the connection or one user closes both directions of data transfer.
    
2. **Graceful connection release:** In the Graceful connection release, the connection is open until both parties have closed their sides of the connection.
    

---

TCP is the protocol that turns an unreliable network into a reliable communication channel by using connections, acknowledgments, retransmissions, and flow control.
