The 3-way handshake

The 3-way handshake

When it comes to the Internet, networking is a very key concept. If we want to communicate we need to be connected to a network. The 3-way handshaking is done generally during the use of the TCP protocol. TCP is used for two reasons: reliability and full-duplex communication i.e. both sender and receiver can send and receive data at the same time.

The three-way handshake is the initial process that TCP uses to establish a connection between a client and server. It involves the following steps:

  1. The client sends an SYN (synchronize) segment to the server to request a connection establishment. This SYN segment contains the client's initial sequence number, source port number, destination port number (let's say 80), SYN flag, window size, and maximum segment size (MSS).

  2. The server responds with a SYN-ACK (synchronize-acknowledge) segment. This segment acknowledges the client's SYN and contains the server's initial sequence number. Also, it sends other information along with an ACK flag.

  3. The client sends an ACK (acknowledge) segment to the server acknowledging the SYN-ACK and containing the next sequence number it expects from the server.

The purpose of the three-way handshake is to synchronize sequence numbers and establish parameters for the TCP connection before transmitting any data. It allows the client and server to agree on:

  • Initial sequence numbers

  • Buffer sizes

  • Window size

  • Other TCP parameters

Once the handshake is complete, the actual data transfer can begin. Each TCP segment sent contains an acknowledgment number, the next expected sequence number, allowing the sender to retransmit any lost packets.

For instance, you can think of a long tunnel that has 2 ends, one at your house and the other opening at your friend's house. Only 2 of you can travel using that path; no one else knows about it. Now, comparing it with the network, TCP is the reliable or secret tunnel that only the sender and receiver know about (in this case you and your friend), and also you guys might have some way to know if the other one is present in their house at the time to establish communication. If no, one friend waits else the other friend acknowledges their presence and you can communicate. This is called the 3-way handshake at a very surface level.

In summary, the three-way handshake is a crucial part of the TCP protocol that allows two endpoints to reliably establish a connection before transmitting data. Synchronizing sequence numbers and establishing TCP parameters ensure a reliable connection.