TCP Working: 3-Way Handshake & Reliable Communication

What is TCP and why it is needed
Imagine sending data on the internet without any rules. Messages could arrive late, out of order, partially, or not at all and the receiver wouldn’t know something went wrong.
TCP (Transmission Control Protocol) exists to prevent exactly this issue.
TCP is a reliable communication protocol. It makes sure that data sent from one computer reaches the other completely, correctly, and in the right order. This reliability is why most important internet activities such as web browsing, API, emails, file downloads depend on TCP.

Problems TCP is designed to solve
Without TCP rules, networks face real problems. Data can be lost due to congestion, packets can arrive in random order, or duplicate packets. Even worse, the sender may never know the receiver missed something. TCP solves these by:
Detecting missing data
Reordering packets correctly
Resending lost packets
Confirming what was received
congestion control
What is the TCP 3-Way Handshake
Before sending actual data, TCP first make a connection. This setup process is called the 3-way handshake. It’s needed for smooth and reliable communication.
It’s like both sides agreeing:
“Oneside: I’m ready to talk”
“Otherside: I can hear you”
“Let’s start”

Step-by-step working of SYN, SYN-ACK, and ACK
SYN (Synchronize)
The client sends a SYN message like saying, “I want to start a connection.” it informs the server that the client is likely to start communication.SYN-ACK (Synchronize + Acknowledge)
The server replies, “I received your request, and I’m ready too.”ACK (Acknowledge)
The client responds, “Confirmed. Let’s begin.”
Now both sides can trust that data can flow safely.

How data transfer works in TCP
It works by establishing a connection via a three-way handshake, breaking data into segments, and using sequence numbers for reordering. Assume it like train with it’s coach, and coach are numbered in proper order for better understanding, b2 coach will come after b1 and if any coach missing, then you will know because of numbering.

Once the connection is established, TCP starts sending data in small chunks called segments. Each chunk is numbered so the receiver knows the correct order.
The receiver sends back acknowledgements(ACKs) to confirm what it received. Only after confirmation does the sender continue smoothly.

How TCP ensures reliability, order, and correctness
TCP constantly checks itself while sending data:
Reliability: If a packet goes missing, TCP notices and resends it.
Order: Even if packets arrive out of order, TCP rearranges them correctly.
Correctness: Corrupted packets are discarded and retransmitted.
How a TCP connection is closed
When communication is complete, It closes the connection effectively to ensure no data is lost. TCP connection is closed using a 4-way handshake process, 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
This happens using FIN and ACK messages:
One side sends FIN: “I’m done sending.”
The other replies with ACK: “I received everything.”
The process repeats from the other side.

Key Aspects of TCP Termination:
Half-Close: Either side can initiate the close independently. A
FINonly closes one direction of traffic, while the other side can continue sending data until it sends its ownFIN.Abrupt Close: If a connection needs to be closed immediately due to an error, a
RST(Reset) packet is sent instead of the 4-way handshake.
Thank you for read!






