How does TCP 3-Way Handshake work ?


Whenever there is some exchange of information between two hardware/software one thing that everyone is aiming for is reliability means that the data has sent successfully without any loss of piece of data in between irrespective of time the main focus is ensuring that the data is pass on properly while also maintaining security throughout the process. Here comes the role TCP 3 way Handshake that ensures all this , In this article we will explore all this so lets dive into it
What is TCP ? (Transmission Control Protocol)
Before understanding the 3-way handshake, let’s quickly brush up on TCP. Transmission Control Protocol, as the name suggests, is a protocol that ensures reliability over speed, and it works on the principle of the 3-way handshake.
Now, you might be wondering, why is this 3-way handshake even required? Let’s break it down. Whenever there’s an exchange of data between two hardware/software, the main goal is to ensure that the data transfers in the correct order.
Wait, what does "correct order" even mean? The data is sent in the form of packets, and these packets are essentially pieces of data with sequence numbers. Sending them sequentially ensures that the data is received in the right order, maintaining reliability. That’s where the 3-way handshake comes in—it’s the process that ensures reliability during data transmission.
How 3 Way Handshake Works ?
Suppose you're on a Zoom meeting, and the first thing you say is, "Hey, can you hear me?" The other person replies, "Yes, I can hear you. Can you hear me?" Notice how the first person confirmed they can hear but also asked, "Can you hear me?" in return. To that, you replied, "Yes, I can hear you." This way, in 3 steps a reliable connection is built. Now, let's merge this analogy with the concept.
So here are the 3 steps :
SYN (Synchronize)
The client sends a SYN (Synchronize) message to the server, requesting a connection. This SYN message also includes a sequence number, which determines the order of data packets to be sent.
SYN(Synchronize) + ACK(Acknowledgement)
Now the Server responds back with SYN (Synchronize) means it also wants to make a connection with the client and sends ACK(Acknowledgment) telling that i have received you information
ACK (Acknowledgement)
The client also acknowledges ACK the server's response, indicating that it has received the SYN-ACK. This final acknowledgment completes the handshake and signals that a reliable connection has been established. At this point, both the client and server are ready to exchange data.
How TCP handles packet loss during transmission ?
Lets first understand the problem here whenever suppose there is data packets transfer happening there number of things can happen that can lead to packet loss
Network Congestion: When too much data flows through the network, routers may drop packets to handle the load.
Hardware Issues: Faulty cables, routers, or switches can cause packet loss.
Signal Interference: In wireless networks, interference from other devices can lead to packet loss.
Routing Errors: Incorrect or unstable routing can cause packets to be lost in transit.
Software Bugs: Malfunctions in network devices or protocols may result in dropped packets
Now the question comes to your mind how the server know or client know there is a packet loss happened so after a packet is sent, the receiver sends an acknowledgment (ACK) back to the sender, confirming that the packet was received successfully. If the sender doesn't receive an ACK within a certain timeout period, it assumes the packet was lost. so don’t worry it is which is known for reliability so here are the ways to handle packet loss
- Retransmissions:
When the sender detects packet loss (due to a timeout or duplicate ACKs), it retransmits the lost packet. This ensures that the missing data is delivered.
Timeouts:
TCP uses a retransmission timeout (RTO). If an ACK for a packet isn't received within the RTO period, the sender retransmits the packet. TCP dynamically adjusts this timeout based on network conditions.Duplicate ACKs:
If the receiver detects a missing packet, it continues to send duplicate ACKs for the last successfully received packet. When the sender gets three duplicate ACKs, it triggers fast retransmission—resending the missing packet without waiting for the timeout.Flow Control:
TCP uses a sliding window protocol to manage the amount of data sent before waiting for an acknowledgment. If packet loss is detected, the window size may be reduced to prevent overwhelming the network.Congestion Control:
TCP assumes packet loss is often due to network congestion (this is just a jargon word it is basically a Network traffic jam). It uses algorithms like Slow Start, Congestion Avoidance, Fast Retransmit, and Fast Recovery to adjust the sending rate and avoid further congestion:
Conclusion
To wrap it up, the 3-way handshake is like a quick check to make sure both sides are ready before sending data. It makes sure everything is in order, and no info gets messed up along the way. I hope you understand the concept much simpler way then what you learned till now
If you got something valuable from it do give it a like ❤
Connect with Me 👇
Subscribe to my newsletter
Read articles from Saurav Jha directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Saurav Jha
Saurav Jha
Software Developer | Full Stack | Testing | API Documentation