THE INTERNET HANDSHAKE: Understanding TCP 3 way handshake

Introduction

The message you send to a friend, the email you need to reply to for your boss, and the endless chats with your friends—all of this is made possible by one key process: the 3-way handshake of the TCP protocol.

In this article, I will dive into the 3-way handshake process. However, before understanding how it works, it's important to first understand what TCP and UDP are and how they differ. I will explain these concepts in detail to give you a clear picture of how the 3-way handshake fits into the bigger network communication process.

Understanding TCP and UDP

TCP and UDP are basically two popular communication protocals used to transfer data between the client and the server, in simple terms it is data transfer protocal of the internet.

TCP

TCP stands for Transmission Control Protocol. The name directly reflects its primary function, which is "Controlling the transmission of data," and in short, this means "Reliability." The main purpose of using TCP is to achieve data reliability, meaning we want the sent data to be transmitted at any cost and in the correct order. By correct order, we mean that the data sent first by the server should be received first by the client, and vice versa.

An example of TCP in action is when you are texting your friend. The messages you send work on the TCP protocol. When you send a message, you expect it to be delivered no matter what. You don't want any message to be lost, and you also want the messages to arrive in the correct order. It would be confusing if your "Good night" message was delivered before your "Good morning" message!

UDP

UDP stands for "User Datagram Protocol." The name reflects "SPEED." or in other terms “LATENCY.” The most important term in UDP is datagram, which is a self-contained, independent packet of data that is sent without needing a connection. Each packet carries enough information to reach its destination on its own, allowing data to be sent quickly. The other two terms are self-explanatory.

A major example of UDP is video calling, where you want to talk to your friend or any other person in real-time. Your audio or video may break in between (loss of data, which is not sent again). In such cases, you typically respond to your friend by saying, "Your audio or video is not clear."

The main point of UDP is speed, without worrying about reliability.

Note: Both of these protocols have their own pros and cons. There is no such thing as one being better than the other; it always depends on the specific requirements of the situation. We need to choose the appropriate protocol based on what the application demands—TCP for reliability and UDP for speed.

TCP guarantees reliability but compromises on latency (resulting in lower performance) , whereas UDP guarantees latency but compromises on reliability.

The 3-way handshake

Now that we have understood TCP and UDP and the differences between them, we can focus on our main topic—the TCP 3-Way Handshake. As discussed in the TCP section, TCP guarantees reliability. This reliability is achieved through the process of the 3-Way Handshake, which establishes a secure and reliable connection between the client and the server.

The 3-way handshake happens between the client and the server. It involves 3 steps:

  1. The client sends a SYN (synchronize) packet to the server to initiate the connection.

  2. The server responds with a SYN-ACK (synchronize-acknowledge) packet to acknowledge the request and synchronize the connection.

  3. The client finally sends an ACK (acknowledge) packet to confirm the connection is established.

To give a simple example of the 3-Way Handshake, we Indians have the habit of sending ₹1 to any new Google Pay (GPay) user before transferring the actual amount. This is similar to the client sending a SYN packet to initiate a payment connection. The new GPay user (acting as the server) responds by confirming that they have received the ₹1 and that the sender can proceed with the full payment, similar to the SYN-ACK. Finally, the sender (client) confirms this and sends the full amount, which is like the ACK step.

Note: The client does not always need to initiate the SYN; it can work both ways. For example, when you browse the Zomato website, the client (your browser) sends a SYN packet to the server to initiate the connection. Conversely, when Zomato sends you notifications about offers, deals, etc., the server initiates the SYN to establish a connection with the client.

Conclusion

In conclusion, the TCP 3-Way Handshake plays a crucial role in establishing a secure and reliable connection between the client and the server. By following the SYN, SYN-ACK, and ACK steps, both sides ensure that they are ready to communicate without any data loss. This process is the reason why TCP is preferred for applications where reliability is important. Whether the client or the server initiates the connection, the handshake makes sure that data transfer happens smoothly and securely.