How does a TCP 3-way handshake work?

How does a TCP 3-way handshake work?

Understand the underlying processes involved in the TCP 3-way handshake

Before diving deep into TCP 3-way handshake, let's first understand what is TCP, in fact, let's start by defining what a network protocol is.

What are network protocols?

When humans talk, we make sure we are communicating with the other person in a language that he/she understands.

Well, computers are no different than us in this scenario. Devices talk to each other over the network using certain protocols to make sure that the communication between the devices is going on smoothly and every device is properly understanding what the other device is conveying.

We can define protocol as:

A standard of communication that allows applications to talk in the same language between different devices.

By language, I don't mean programming languages like C++, Python, JS, etc. what I mean instead are protocols like HTTP, FTP, and SMTP.

What is TCP?

TCP stands for Transmission Control Protocol. It belongs to the Transport Layer of the TCP/IP model.

Here are some features of TCP:

  • End-to-end session is established between two endpoints
  • Due to this, it reliably sends data.
  • In case of failures, re-transmission of the data is done.
  • When the data transmission is complete, the session is terminated

Now, coming back to the title, TCP 3-Way handshake.

When we make a request (let’s say a GET request), this request first gets converted into network packets.

These packets then arrive at the server. The server will assemble these packets and will analyze, that alright I have got a GET request from the client, I am gonna process it now.

TCP-3-Way-Handshake-Page-1.png

But in the process of transmitting these packets from the client to the server, it may happen that some of the packets might get lost i.e they do not reach the server. So, how do we keep a track of which packets reached the server and which packets were lost?


This is where the concept of acknowledegment kicks in.

TCP-3-Way-Handshake-Page-2-border.png

For each packet that the server receives, it has to send back an acknowledegment to the client that hey, I have successfully received this packet.

And for those packets for which the client doesn't receive an acknowledegment, they are simply re-transmitted to the server. A cool feature of TCP, right?

Ok, but how do we identify which packet was lost and which packet was received. For this, each packet has a sequence number associated with it and in the acknowledegment that we receive from the server, the sequence number of the packet is also mentioned.

TCP-3-Way-Handshake-Page-3-border.png

So far so good. But how does the server know which all packets were required to be present in the request in order to process it properly? There has to be a synchronization between the client and the server, that would tell the server that these sequence numbers must be present in the request that you would receive so that the request can be processed properly.

So in order to achieve this synchronization, the TCP 3-Way Handshake occurs.

TCP 3-Way Handshake



Here’s what happens in TCP 3-Way Handshake:

  1. First the client will send a request to the server indicating the sequence number present on the client-side. It is called the SYN request.

  2. Now, when the server receives this SYN request from the client, it responds with the SYN-ACK response. This SYN-ACK response informs the client about the sequence number present on the server-side and also acknowledges the client that the server successfully received the SYN request that the client sent before in step 1.

  3. Now, when the client receives this SYN-ACK response from the server, it again sends back an acknowledgment to the server that it has successfully received the sequence number of the server that the server sent in the previous SYN-ACK response in step2.

This is what a TCP 3-Way handshake looks like.

TCP-3-Way-Handshake-Page-5.png

The TCP session is established only after the completion of the TCP 3-Way handshake. Once the TCP session is established, then the actual GET request is made to the server to fetch data for the client.

I hope I have been able to explain the underlying concepts of the TCP Handshake in an easy to comprehend manner. If you have any doubts or if I have wrongly mentioned anything, please drop a comment below. I would love to address those.

Also, feel free to connect with me on LinkedIn.

Cheers! 🤘