tcp three way handshake

 What is TCP three-way handshake? 

We know that TCP is an example of the implementation of the transport layer protocol according to the OSI model. The protocol is connection-oriented, means before sending any data to the remote peer, TCP client set up a virtual connection over a packet-based underlying IP network.  The three-way handshake is the protocol procedure to set up both way connection with the peer TCP. Here we will cover the TCP connection setup procedure in detail.  The first question comes in mind, who is responsible for starting a TCP connection?

The protocol layer itself, on startup or some other external entity, instructs the layer for connection? The answer is TCP user is responsible to start a TCP three-way handshake. For example, HTTP (web browser uses HTTP), is a user of TCP. When HTTP user needs to send a web request to the remote server.

Before sending any user data. The HTTP requests,  TCP layer to create a new connection with the remote server. Once the TCP layer receives a connection request from HTTP, it starts TCP 3 way handshake. Like any other protocol, the three-way handshake procedure requires to exchange protocol messages between client and server.  The following are messages in the connection setup procedure.

Following is the message flow for a three-way handshake.tcp three way handshake

The user of HTTP issues a connect request to the TCP layer. TCP layer works as a TCP Client and sends the TCP syn with an initial sequence number. The sequence number is to maintain the sequencing of messages. Because TCP delivers messages to the user in sequence.

Upon SYN received Sever sends a new syn and an ack for a received SYN to the client, then the client sends the ACK to the server for a received SYN from the server. This completes the connection setup. The following mentions each message in detail for a three-way handshake.

TCP SYN packet:

This is the first packet from the client to the server. TCP message set SYN flag to 1 in the message, so make the TCP message as SYN segment. It has the initial sequence number of the client along with other few more parameters.

TCP SYN-ACK packet:

After receiving the SYN packet, the server sends the syn ack packet to the client. Not to mention that this is a single TCP packet with syn and ack bit set to 1. The syn sequence number is the initial sequence number of the server accepting the connection.  The ack part has the client sequence number plus one. This way server tells that it is ready to accept the packet with the next sequence number from the client.

TCP ACK packet:

The final packet for the connection setup is TCP ack. The client sends a TCP ack packet upon receiving TCP syn ack from the server. The packet includes a sequence number from the server plus one.

TCP user indication after three-way handshake : 

At the start, we have mentioned that it is the user who initiates a connection request. But how the user gets,  that connection is done? And the user can use the connection to send data to a remote server?  The user gets an indication of the connection setup result from the TCP layer.  If the handshake is successful, the TCP user gets the connection identifier. Else and error.  Connection identifier works as a handler for sending/receiving data to/from the server. We will show in another post about the exact implementation of the TCP client/server, then you can get a more clear picture of the connection handler.


Leave a Comment