top

Search

Software Key Tutorial

.

UpGrad

Software Key Tutorial

TCP 3 Way Handshake

Introduction

In the dynamic world of the digital realm, billions of devices come together to create a symphony of interconnectivity. In this symphony, an unseen conductor coordinates data interchange to create a harmonious interaction among the many signals. The master is TCP, the Transmission Control Protocol. 

The secret heartbeat of this magnificent ensemble is the TCP 3-way handshake process. It ensures reliable data transit between devices, which underpins our digital lives. However, many of us wonder what this mechanism is and why it's so important in our digital lives. Explore the fascinating world of TCP and the 3-way handshake.

Overview

Networking allows devices to communicate. For this communication to be successful, both devices must establish a mutual understanding. That's where the TCP handshake mechanism comes into play. In this article, we'll dive deep into the world of TCP, starting from its basics and moving on to its advanced features.

What is TCP?

TCP stands for Transmission Control Protocol. It is one of the main protocols used in the Internet protocol suite. Imagine two friends, Alice and Bob, wanting to talk over the phone. Before starting their conversation, they'd first ensure the other person is available and ready to talk. Similarly, TCP ensures that both the sending and receiving devices are ready to exchange data. Examples of applications using TCP include web browsers and email services. However, without visual aids such as screenshots or images, it's hard to showcase them here.

What is TCP 3 Way Handshake?

Transmission Control Protocol (TCP) uses the 3-way handshake to establish a secure connection between two networked devices. This connection-oriented protocol guarantees that both parties are prepared to transmit data and have agreed upon the communication parameters. The greeting consists of three steps:

1. SYN (Synchronize):

The initiating device, also known as the client, begins the procedure by sending a packet containing the SYN (synchronize) flag. This signifies that the consumer wishes to establish a connection. The packet also includes an initial sequence number, which the client uses to monitor the order of the data packets it sends.

2. SYN-ACK (Synchronize-Acceptance):

The receiving device, typically the server, responds to the SYN packet with a SYN-ACK packet. This packet acknowledges the SYN request made by the client and contains:

- An acknowledgment number that is one greater than the initial sequence number transmitted by the client.

- A distinct SYN flag paired with the server's initial sequence number indicates the server's intent to synchronize its data packet sequencing with the client.

3. ACK (Acknowledge):

In the final stage, the client sends an ACK packet responding to the server's SYN request. The acknowledgment number in this packet is increased by one from the sequence number received in the SYN message from the server. Once the server receives this acknowledgment, the connection is deemed established, and data transfer can commence between both devices.

Before data is exchanged, the TCP 3-way handshake confirms mutual agreement between two devices to communicate, establishes appropriate sequencing for data packets, and ensures a reliable connection. This procedure is crucial for ensuring the reliable transmission of data across networks.

3-Way Handshake in TCP: A Detailed Example

Consider attending a formal event, such as a ballroom dance. Establishing a dance partnership in this context is analogous to the TCP 3-way handshake used to establish a connection between two devices. Let’s break down the steps using the analogy of dance to explain the tcp 3-way handshake mechanism.

Step 1. The Invitation (SYN):

  • What actually happens in TCP: 

The client sends a SYN (synchronize) message to the server when attempting to establish a connection. This message includes specific details such as the initial sequence number, window size, and maximal segment size.

  • Ballroom dance as an analogy:  

You (the client) observe a potential dance partner (the server). You approach them with the intent of inviting them to dance, indicating your wish to do so. This is your introduction or "synchronization" with your prospective dance companion.

Step 2. The Acknowledgment (SYN-ACK):

  • What actually happens in TCP:

Upon receiving the client's SYN message, the server returns a message consisting of two parts: an acknowledgment of the client's SYN (known as the ACK) and the server's own SYN message. The server states, "I acknowledge your request to dance, and I'm also ready to dance with you." The acknowledgment number will be one greater than the initial sequence number assigned to the client. Additionally, the server will transmit its own sequence number, window size, and additional parameters.

  • Ballroom dance as an analogy:  

The individual you invited (the server) acknowledges your invitation. They acknowledge with a nod and extend their hand, signifying they are also eager and prepared to dance. The hand gesture is analogous to the server's SYN message, and the motion signifies acknowledgement.

Step 3. Final Acknowledgement (ACK):

  • What actually happens in TCP:

The client transmits an acknowledgment (ACK) of the server's SYN message after receiving the server's SYN-ACK message. Again, the acknowledgment number will be one greater than the server-provided sequence number. Once this is accomplished, the connection is entirely established, and data can be exchanged between the parties.

  • Ballroom dance as an analogy:  

You (the customer) accept the offered hand and give a final gesture to indicate that you are now entirely prepared to dance. With this final acknowledgment, you and your partner move in sync on the dance floor as data travels between the client and the server.

Explanation:

Before exchanging data, the TCP 3-way handshake meticulously verifies that both participants in digital communication are in sync. In the digital sphere, the handshake ensures that both the client and server are synchronized and prepared for a seamless data exchange, much like in our ballroom dance, where mutual readiness is essential before the start of the dance.

Layers of TCP

TCP operates at the transport layer of the OSI model. This layer is responsible for ensuring end-to-end communication and error recovery. To understand this better, think of sending a letter through the post. The letter (or data) is placed in an envelope (the TCP layer) with both the sender's and receiver's addresses. This ensures the post reaches the right person and can be returned if there's an issue.

TCP Segment Structure

A TCP segment's backbone is its header, which can range between 20 to 60 bytes. The crucial components of the TCP header are:

- Source port and Destination port: These two 16-bit values, which function similarly to street addresses, define where data is coming from and where it is headed.

- Sequence Number: This is the number that maintains the order of the bytes. Think of it as numbering the pages in a book; the sequence number does something quite similar for data.

- Acknowledgment number: This is the server's way of stating, "I've received your message; now waiting for the next one."

- Header Length (HLEN): This determines how long the header is in order to get the data processing started as soon as possible after it.

- TCP Flags: These are control bits or indications. These are URG, ACK, RST, PSH, SYN, and FIN.

Checksum refers to a sequence that can be used to detect any data mistakes.

- Window size: This is the amount of data that can be transferred before waiting for an acknowledgment from the recipient.

- Urgent pointer: An indicator for data that is considered urgent is known as an urgent pointer.

TCP Termination (A 4-way handshake)

Now, what if our two devices want to stop communicating? This termination can sometimes involve a TCP 4-way handshake. 

Let’s consider the previous example of a ballroom dance.

1. The Dance Pause (FIN from the client): As the dance progresses, one coder (acting as the client) determines to terminate the session. This choice is comparable to a break statement in a loop. Similar to setting the FIN (complete) flag, the programmer takes a step back and signals. This action is analogous to a function's return statement, signifying the intention to exit and return control.

2. Acknowledging the Pause (ACK from the server): Upon observing this, the partner coder (or server) analyzes this 'input' and responds, analogous to activating a callback function. This coder sends an acknowledgment, comparable to an ACK (acknowledge) packet, to confirm that they have received the termination request. Think of this as a software validation message that states "Changes have been saved."

3. The Final Bow (FIN from the server): The Ultimate Bow (FIN on the server):

Now, the co-developer decides to complete their work. This is the equivalent of calling the end() function of a file or stream. By transmitting their own FIN packet (or making their own 'function call' to terminate), they indicate that they are prepared to end the dance.

4. The Last Acknowledgment (ACK from the client): Upon receiving this, the initial programmer confirms the partner's intent, analogous to the .commit() action in databases, thereby solidifying the decision to terminate. The final acknowledgement is transmitted, resembling a successful console log in a programming environment that confirms "Connection terminated successfully."

Conclusion

In conclusion, the TCP 3-way handshake is crucial in managing connections for various internet operations we commonly use. Operating like an invisible conductor, it orchestrates the seamless data flow between devices, facilitating multiple activities such as casual web browsing, essential email communications, and extensive file transfers. It ensures a harmonious, reliable, and orderly dialogue within the chaotic realm of cyberspace. While its role may frequently go unnoticed, its significance cannot be overstated. By comprehending the intricate dance of synchronizing and acknowledging, we gain insight into the fundamental processes that enable our interconnected world to exist.

FAQs

1. What is the main purpose of the TCP 3-way handshake?

   The primary objective of the TCP 3-way handshake is to establish a reliable connection between a client and a server to ensure successful data transfer.

2. Is there such a thing as a 2-way handshake?

   While the standard handshake in TCP is 3-way, some protocols, especially non-reliable ones, might use a simpler 2-way handshake mechanism. However, it doesn't provide the same level of reliability as the 3-way handshake.

3. What role do TCP identifiers play in the TCP handshake procedure?

   TCP identifiers such as SYN, ACK, and FIN are crucial because they determine the state and nature of the connection, i.e., whether it is establishing, continuing, or terminating.

4. Is it possible to terminate a connection using a 3-way handshake?

   While the establishment typically employs a three-way handshake, terminations may employ either a three-way or four-way handshake, depending on the circumstances.

Leave a Reply

Your email address will not be published. Required fields are marked *