TCP working : 3-Way Handshake & Reliable Communication

On the internet, millions of data packets are sent between computers. Imagine if data sent without any rules, data packets can be lost, data arrives in wrong order, or get corrupted. To avoid these problems, we need proper protocols that controls how data is send and received.
What is TCP and why it is needed
TCP(Transmission Control Protocol) is a communication protocol used to send data reliably over the internet. It is designed to send data safely from one computer to another. Without TCP, data can be lost, duplicated, or mixed in wrong order.
TCP is needed because applications like websites, APIs, file downloads, emails require all data arrives successfully, data arrives in the correct order, missing data is retransmitted.
Without TCP web pages could load partially, files could become corrupted, APIs could return incorrect data.
Problems TCP Designed to Solve
TCP is designed to fix common problems that happen when data travels over the internet.
Data loss - Some data packets may get lost on the way.
Out-of-order- data Packets may arrive in the wrong order.
Duplicated Data - The same packet may arrive more than once.
Unreliable delivery - No guarantee that data reaches the destination.
TCP solves these problems by tracking data packets, resending lost data, and delivering data in the correct order.
What is the TCP 3-Way Handshake
The TCP 3-Way Handshake is the process TCP uses to establish a reliable connection between client a and a server before sending data.
It makes sure that both sides are ready to communicate.
You can think of it like a simple conversation before starting to talk.
Client: Can we start a connection.
Server: Yes, I’m ready.
Client: Great, let’s start sending data.
After this, the TCP connection is successfully established.
Step-by-step working of SYN, SYN-ACK, and ACK
First step - SYN(Synchronize)
The client sends a SYN message to the server.
This means: I want to start a connection.
Second step - SYN-ACK(Synchronize + Acknowledge)
The server replies with SYN-ACK.
This means: I recieved your connection request and I am ready to connect.
Third step - ACK(Acknowledge)
The client sends an ACK message back to the server.
This means: I recieved your response. Let’s start sending data.
After this process, a secure TCP connection is created.

How data transfer works in TCP
After the TCP 3-way handshake, the connection is established and data transfer begins.
TCP splits large data(file, webpage, API response) into small packets.
Packets are sent with sequence numbers, each packet gets a sequence number so the receiver knows the correct order if any packet is missing.
After receiving packets, the receiver send ACK messages, I received this packet.
If a packet is lost or not acknowledged TCP resends it automatically.
The receiver puts all packets back together using sequence numbers.
How TCP ensures reliability, Order, and correctness
Reliability: TCP makes data reliable by Acknowledgements, the receiverconfirms each packet it receives,
if an ACK is not received, TCP resends the packet.
If data is lost,TCP sends it again.
Order: TCP ensures correct order using Sequence numbers on every packet. Even if packets arrive out of order TCP rearranges them correctly, delivers data to the application in the right order.
The application always sees data in sequence.
Correctness: TCP ensures correctness by Error detection, Detecting corrupted packets, Requesting retransmission of damaged data.
Corrupted data is never passed to the application.
