TCP 3-Way Handshake

The TCP 3-way handshake is  used to establish a connection between a client and a server in a TCP/IP network. It’s the initial step before data transmission begins. Here’s how it works:

TCP Three-Way Handshake Process

redeemnetworks.com

TCP traffic begins with a three-way handshake. In this TCP handshake process, a client needs to initiate the conversation by requesting a communication session with the Server.

  • Step 1: In the first step, the client establishes a connection with a server. It sends a segment with SYN and informs the server about the client should start communication, and with what should be its sequence number.
  • Step 2: In this step server responds to the client request with SYN-ACK signal set. ACK helps you to signify the response of segment that is received.  SYN signifies what sequence number it should able to start with the segments.
  • Step 3: In this final step, the client acknowledges the response of the Server, and they both create a stable connection will begin the actual data transfer process.

How it works

  1. SYN (Synchronize): The process begins with the client (let’s call it Client A) sending a TCP segment with the SYN flag (SYN = Synchronize) set to the server (Server B). This segment contains a sequence number that Client A chooses.
  2. SYN-ACK (Synchronize-Acknowledge): Upon receiving the SYN segment, Server B responds with a TCP segment that has the SYN and ACK (Acknowledge) flags set. The acknowledgment number is set to one more than the sequence number that Server B received from Client A. Additionally, Server B selects its own sequence number.
  3. ACK (Acknowledge): Finally, Client A responds to Server B with an ACK segment. The ACK flag is set, and the acknowledgment number is set to one more than the sequence number that Server B sent in the SYN-ACK. Client A also confirms the receipt of Server B’s sequence number.

Once this 3-way handshake is completed successfully, a TCP connection is established between Client A and Server B. They can now begin exchanging data packets in both directions until the connection is closed.

This process ensures that both sides are ready and capable of receiving and sending data before the actual data transfer begins, enhancing reliability and integrity in communication.

TCP message types

Message Description
Syn Used to initiate and establish a connection. It also helps you to synchronize sequence numbers between devices.
ACK Helps to confirm to the other side that it has received the SYN.
SYN-ACK SYN message from local device and ACK of the earlier packet.
FIN Used to terminate a connection.

Three-Way HandShake or a TCP 3-way handshake is a process which is used in a TCP/IP network to make a connection between the server and client. It is a three-step process that requires both the client and server to exchange synchronization and acknowledgment packets before the real data communication process starts.

Three-way handshake process is designed in such a way that both ends help you to initiate, negotiate, and separate TCP socket connections at the same time. It allows you to transfer multiple TCP socket connections in both directions at the same time.

Real world examples for TCP

  • Web Browsing (HTTP/HTTPS): When you visit a website, your browser and the web server communicate using TCP. For instance, when you type a URL and hit enter, your browser initiates a TCP connection to the server on port 80 (HTTP) or port 443 (HTTPS), and then data (web pages, images, etc.) are transmitted over this connection.
  • Email (SMTP/POP/IMAP): Sending and receiving emails often involves TCP connections. SMTP (Simple Mail Transfer Protocol), POP (Post Office Protocol), and IMAP (Internet Message Access Protocol) are used for sending and retrieving emails. These protocols rely on TCP for reliable data transfer.
  • File Transfer (FTP/SFTP): FTP (File Transfer Protocol) and its secure variant, SFTP (SSH File Transfer Protocol), utilize TCP connections for transferring files between a client and a server securely.
  • Remote Access (SSH, Telnet): Secure Shell (SSH) and Telnet are protocols used to remotely access computers or servers. TCP is used for establishing connections in both SSH (encrypted) and Telnet (unencrypted) sessions.
  • VoIP (Voice over Internet Protocol): Services like Skype, Zoom, and other VoIP applications use TCP to ensure reliable transmission of voice data packets over the internet.
  • Online Gaming: Many online games rely on TCP to transmit game data between players and game servers, ensuring that actions and movements are accurately communicated.
  • Database Access (MySQL, PostgreSQL): Database systems often use TCP connections for clients to connect and query databases, ensuring data integrity and reliability.

In these scenarios, TCP’s reliability, error-checking, and sequencing mechanisms are crucial to ensure that data is transmitted accurately and in the correct order.

The Four-way Handshake or TCP Connection Termination:

Here, to terminate an established TCP connection, the following 4 TCP packets are needed to be exchanged. Which you can understand with the statements below:

1. PC –A → PC B: FIN flag set.
2. PC -B → PC A: ACK flag set.
3. PC– B → PC A: FIN flag set.
4. PC– A →PC B: ACK flag set.

These 4 steps are known as a TCP 4-way handshake, which is necessary to terminate a TCP connection. For the termination of the established TCP connection, the following steps are necessary for the process. Which are given down below:

  1. Firstly, from one side of the connection, either from the client or the server the FIN flag will be sent as the request for the termination of the connection.
  2. In the second step, whoever receives the FIN flag will then be sending an ACK flag as the acknowledgment for the closing request to the other side.
  3. And, at the Later step, the server will also send a FIN flag as the closing signal to the other side.
  4. In the final step, the TCP, who received the final FIN flag, will be sending an ACK flag as the final Acknowledgement for the suggested connection closing.

Since the Four main steps are required to close an active connection, so, it is called a four-way handshake.

Summary

  • TCP 3-way handshake or three-way handshake or TCP 3-way handshake is a process which is used in a TCP/IP network to make a connection between server and client.
  • Syn use to initiate and establish a connection
  • ACK helps to confirm to the other side that it has received the SYN.
  • SYN-ACK is a SYN message from local device and ACK of the earlier packet.
  • FIN is used for terminating a connection.
  • TCP handshake process, a client needs to initiate the conversation by requesting a communication session with the Server
  • In the first step, the client establishes a connection with a server
  • In this second step, the server responds to the client request with SYN-ACK signal set
  • In this final step, the client acknowledges the response of the Server
  • TCP automatically terminates the connection between two separate endpoints.

Leave a Reply