# TCP ( Transfer Control Protocol ) & 🤝

Tcp is one of the main protocols of the Internet protocol suite . TCP provides reliable, ordered, and error-checked delivery of a stream of octets (bytes) between applications running on hosts communicating via an IP network.

---

## Problems solved via TCP

By the definition above you can point out most of the problems solved by TCP

* Loss of Data packets
    
* Unordered data reached at destination
    
    Rest are
    
* Data corruption
    
* Duplicate data packets
    
* Network Congestion
    

---

## 3 Way HandShaking

Now lets discuss the important procedure known as 3 way handshake .

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769443635430/4328cce7-5c0d-48fc-9d0d-d9a258dde3f0.png align="center")

As the name , it really is a 3 way talk between client and server

1. Clients sends data packets that is called SYN ( Synchronize )
    
2. Server sends data packets as response and acknowledgement
    
3. then client sends the last acknowledgement that it has received
    

Its like taking online meeting Host tells you to speak → you ask if voice is coming → then host answers and then the conversation begins.

---

## HOW TCP ensures

![](https://www.tutorialspoint.com/assets/questions/media/51928/Page-64-Image-106.jpg align="left")

### Reliable Transmission

TCP uses a *sequence number* to identify each byte of data. The sequence number identifies the order of the bytes sent from each computer so that the data can be reconstructed in order, regardless of any out-of-order delivery that may occur. The sequence number of the first byte is chosen by the transmitter for the first packet, which is flagged SYN. This number can be arbitrary, and should, in fact, be unpredictable to defend against TCP sequence prediction attacks.

Acknowledgments (ACKs) are sent with a sequence number by the receiver of data to tell the sender that data has been received to the specified byte. ACKs do not imply that the data has been delivered to the application, they merely signify that it is now the receiver's responsibility to deliver the data.

Reliability is achieved by the sender detecting lost data and retransmitting it. TCP uses two primary techniques to identify loss. Retransmission timeout (RTO) and duplicate cumulative acknowledgments (DupAcks).

---

## 4 way Handshake

you must be wondering this is some upgraded version for establishing connection but sadly it is a procedure followed for terminating the connection.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769445251725/6f89e65b-627f-4d90-8cbd-d4a1f11a3f80.png align="center")

FIN = finish packet

ACK = acknowledge packet

When an endpoint wishes to stop its half of the connection, it transmits a FIN packet, which the other end acknowledges with an ACK. Therefore, a typical tear-down requires a pair of FIN and ACK segments from each TCP endpoint. After the side that sent the first FIN has responded with the final ACK, it waits for a timeout before finally closing the connection, during which time the local port is unavailable for new connections; this state lets the TCP client resend the final acknowledgment to the server in case the ACK is lost in transit.

---
