What is the TCP three-way handshake?
The TCP is an example implementation of the transport layer protocol according to the OSI model. It connection-oriented, which means before sending any data to the remote peer, the TCP client sets up a virtual connection over a packet-based underlying IP network.
A three-way handshake is a protocol procedure to set up a both-way connection between two peers. The tutorial will cover the TCP connection setup procedure in detail.
The first question comes to 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 initiation? The answer is TCP user is responsible to trigger the TCP three-way handshake. For example, HTTP ( a web browser such as chrome uses HTTP), is a user of TCP. When the 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 of protocol messages between client and server.
The following are messages in the connection setup procedure.
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 a 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. The connection identifier works as a handler for sending/receiving data to/from the server. We will show in another post the exact implementation of the TCP client/server, then you can get a more clear picture of the connection handler.