Skip to main content

Command Palette

Search for a command to run...

TCP Working: 3-Way Handshake & Reliable Communication

Published
7 min read

Imagine two people trying to talk to each other at the exact same time without taking turns. One starts speaking before the other finishes. None of them wait for confirmatoin and sometimes one person doesn’t even know if the other guy is listenign or not. Messages gets iterrupted, some information is skipped or missed. It will not take a lot of time for this conversation to become confusing really quickly.

Now imagine the same situation but between two computers. When your laptop sends some data or request to the server, it does not send a single block of information packing all the necessary data in it. It sends small pieces of data across network often known as packets. These pieces travel through routers, swithces, cables and wireless signals. Now it is not necessary that all the packets come in order or from the same path. On the way some pieces can get delayed, some can get lost or out of order.

What Happens Without Rules

If there were no rules, the recieving computer would have not idea about whih piece of data came first, what pieces are missing, whether or not the data is corrupted, if the sender is even ready to resend something or if it even knows that the data has been recieved.

Without any rules or structure digital communication would be very unrelieable. Websites might load halfway and then stop, files could arrive corrupted and messages or signals might mix together. Compiuters need clear rules to communicate properly because of the above mentioned concerns. They need some kind of system that organizes the conversaiton, confirms delivery and fices problems automatically. And hence Transmission Control Protocol (TCP) was born.

What is TCP and Why It Is Needed

Imagine writing some important message on some papers. Now you want to send this message to your friend but you toss the papers in the air for them to catch. Some papers might fall away, some arrive out of order and some get completely lost. This is more or less exactly what will happen if the internet had not rules for sharing data between computers.

TCP or Transmission Control Protocol is a way to minimize data loss as it is a set of rules to share data over the internet. It is one of the core protocols of the internet. Its job is to make sure that data is delivered reliably from one device to another. TCP is running behind the scenes when you open a website, send an email, download or dounload a file to ensure that everything arrives safely, correctly and complettely.

Without TCP the internet will be unreliable. Web pages would not load completely, files can be corrupted. Messages could mix together etc. TCP provides structure, order and reliability when communicating between two computers.

What is the TCP 3-Way Handshake?

Imagine you are on a phone call with a friend. Before you both start talking about anything you first dial, the other person answers and then you say “Hello There!” (For my star wars nerds) to confirm if you’re both on the line or not. This back-and-forth checks if both people are able to listen over the phone or not. This is exactly what a TCP 3-Way handshake does but between two communicating computers.

In the vast world of networking when one computer wants to talk to another they can’t just start sending data blindly. They first need to make sure that a secure connection is established ad agree to communiate and synchronize some basic sinformation so that the messages can be send and understood reliably.

In the context of computers, Hansdshake means sharing data to start a coversation and 3-Way implies that there will be exaclty 3 messages between the two computers. These messages usually carry control information that helps synchronize the connection. At thte end of this handshake both sides know the other is ready, Both have agreed on packet indexing, i.e. starting numbers that help puting data in order. A realiable connection is established for actual data to flow.

You might imagine a 3-Way handshake like two computers saying
CLIENT: “Hello, are you listening?”
SERVER: “Hi, Yes I am!”
CLIENT: “Alright, let’s begin”

Step-by-Step: SYN, SYN-ACK, ACK

As dicussed above before two computers start sending real data they kind of introduce themselves and agree tot alk. Let us see the technical jargons associated with the idea of a 3-Way handshake.

SYN

When a client wants to connect to a server, it sends the first message called a SYN packet. SYN stand for synchronization. This packet conatins a message that is like “Hey server I want to connect”. But more importantly it includes a sequence number. This number is supposed to mark the starting of the sequence number fomr where we start counting numbring the packets.

SYN-ACK

Once the sever recieves the clients SYN, the servers responds with a combined message called SYN-ACK. Here ACK stands for Acknowledgement. This message has two parts.

  • SYN: The server is ready to connect

  • ACK: The server got the initial request

The server also includes its own sequence number, starting a separate count for its outgoing data.

ACK

Now backa at the client system again after recieveing the SYN-ACK message, it sends a final ACK message. This here means that the client server hand established a connection and are ready to share data.

How TCP Ensures Reliability, Order, and Correctness

Once a TCP connection is established after a 3 way handshake, the computers do not start transmitting data and hope for the best. TCP takes special steps to make sure that the data you send actually reaches the other side intact, in the right order and without any loss.

Reliability

Imagine you are mailing some hand written notes to a friend. Some notes may get lost in the mail, you would want your friend to acknowledge what mails he did get so you can resend the ones that did not reach. This is exactly what TCP does using acknowledgements (ACK). Every chunk of data segment that TCP sends has a sequence number. The reciever sends back ACK message for every sequence number. If the server doe not see an ACK in a set time, it assumes that the data was lost and sends that segment again.

Order

Networks don’t guarentee that packets will arrive in the order they were sent in. TCP solve this by nunbering each byte of data with a sequence number. The reciever uses those number to reassemble the data in the correct order before passing it up to the application.

Coorectness

Even if data arrives, it might get corrupted in transit. To protect against this TCP uses checksum. Checksum is basically any mathematical relation. Before sending a segment TCP calculates a simple mathematical summary of the data. upon recieving the data this checksum is calculated again. If the sender and the reciever checksum match then the data arrived was correct otherwise not. This is how the server knows if the data is corrupt and if it needs a retransmission or not.

How a TCP Connection Is Closed

When your browser is done loading a page or an app is done talking to a server the TCP connection needs to be closed. Think back to our “Two people talking over a call” example, they do not hang up mid sentence if the conversation is over. Each person says bye before hanging up making sure the other side heard everything. TCP does something similar, this final exchange is called conection termination.

TCP is a full duplec protocol, this means that data can flow in both directions. This is why each side must tell the other when it has finished sending the data. If just one side closes the connection, it will remain is a half open state wasting system resources for either ends.

TCP 4 Step Closing

Unlike the 3-Way handshake for establishing a connection, Closing usually involves 4 steps:

  1. FIN (Finish): One side says it is done sending the data

  2. ACK (Acknowledge): The other side recieves the FIN and acknowledges it

  3. FIN: Now the other side sends a final message saying they’re done sending data.

  4. ACK: Finally the initial side acknowledges the final mesasge

This closes the TCP connection for both the parties involved.

Conclusion

Every time you open a website, TCP quietly manages a complete conversation in the background from the three-way handshake to reliable data transfer to closing the connection. It ensures data arrives safely, in order, and without confusion. All of this happens in milliseconds (isn’t it mind blowing, that like less than the time it takes for you to blink), making the internet feel fast and seamless.

More from this blog