TCP handshake and 3 way handshakes

·

6 min read

The TCP handshake, often referred to as the 3-way handshake, is the process that establishes a reliable connection between a client and a server in a TCP/IP network. It ensures that both sides are ready to communicate and that the connection parameters are correctly set up.

The 3-Way Handshake Process:

  1. SYN (Synchronize):

    • The client sends a TCP packet with the SYN flag set to the server, requesting to establish a connection.

    • The packet contains the initial sequence number (ISN) of the client.

  2. SYN-ACK (Synchronize-Acknowledge):

    • The server responds with a packet that has both the SYN and ACK flags set.

    • The server acknowledges the client's SYN request by setting the acknowledgment number to the client's ISN + 1.

    • The server also sends its own ISN.

  3. ACK (Acknowledge):

    • The client sends a final ACK packet to acknowledge the server's SYN-ACK response.

    • The acknowledgment number is set to the server's ISN + 1, indicating that the client is ready to proceed with communication.

TCP 3 way handshakes

The TCP 3-Way Handshake is the process that ensures both the client and server are synchronized and ready for data transmission. It involves three steps, where each side acknowledges the other, ensuring both are ready and establish a reliable connection.

Here's a breakdown of the 3-Way Handshake:

1. SYN (Synchronize):

  • Client → Server: The client sends a TCP packet with the SYN (Synchronize) flag set to initiate the connection.

  • The packet includes the client's Initial Sequence Number (ISN), which is a random number that will be used for the sequence of data transmission.

2. SYN-ACK (Synchronize-Acknowledge):

  • Server → Client: The server responds with a TCP packet that has both the SYN and ACK (Acknowledge) flags set.

  • The server acknowledges the client's SYN request by setting the Acknowledgment Number to Client ISN + 1.

  • The server also sends its own Initial Sequence Number (ISN).

3. ACK (Acknowledge):

  • Client → Server: The client sends an ACK packet to the server, acknowledging the server's SYN-ACK.

  • The Acknowledgment Number is set to Server ISN + 1, confirming that the client received the server’s sequence number.

How the Internet Says 'Hello'

When the internet "says hello," it typically refers to the process of establishing communication between two devices over a network, which often starts with the TCP handshake or an initial request. This is how devices on the internet initiate a connection to exchange data.

Here’s how this works in simple terms:

  1. Client Request (Saying "Hello"):

    • When you visit a website, your web browser (the client) sends an HTTP request to the server that hosts the website.

    • This request is like saying, "Hello, I want to communicate with you."

    • The browser might first establish a TCP connection (via the 3-way handshake) to the server to ensure they can reliably send and receive data.

  2. DNS Resolution (Finding the Address):

    • Before saying "hello" to the server, your device needs to know the server's IP address (its unique identifier on the internet).

    • This is done through the DNS (Domain Name System), which translates the human-readable domain (like www.example.com) into an IP address (like 192.168.1.1).

  3. TCP Handshake (Establishing a Conversation):

    • The 3-way handshake (described earlier) between the client and the server establishes a reliable communication channel, ensuring that both devices are ready to exchange data.
  4. HTTP Request (Sending a Message):

    • After the handshake, the client sends an HTTP request (like a "GET" request for a webpage) to the server, asking for the specific resources (webpage, image, etc.).

    • This request can be thought of as saying, "Hello, here’s what I need!"

  5. Server Response (Replying to the Hello):

    • The server replies with an HTTP response, which contains the requested data (like the HTML content of a webpage).

    • This is the server’s way of saying, "Hello, here’s the information you asked for."

  6. SSL/TLS Handshake (for Secure Communication):

    • If the communication is over HTTPS (secure HTTP), there’s an additional step before the data is exchanged: the SSL/TLS handshake.

    • This ensures that the communication is encrypted, allowing for a secure "hello" where both parties agree on how they will communicate securely.

So, the internet says hello through a series of steps:

  1. DNS: Resolves the address.

  2. TCP Handshake: Ensures both devices are ready.

  3. HTTP Request/Response: Actual communication (the "hello" and "reply").

Reliable Connections in TCP

A reliable connection in TCP (Transmission Control Protocol) ensures that data is transmitted accurately, in order, and without loss between the sender and the receiver. This reliability is crucial for many internet-based applications, such as web browsing, file transfers, and email, where data integrity and order are essential.

Here's how TCP achieves a reliable connection:

1. Connection Establishment (3-Way Handshake)

  • Before any data is transmitted, TCP establishes a connection between the client and server using the 3-way handshake (SYN → SYN-ACK → ACK). This step ensures both sides are ready for communication and agree on sequence numbers to track the data.

2. Data Segmentation

  • Data is divided into smaller chunks called segments.

  • Each TCP segment is assigned a sequence number, which helps the receiver know the order in which the segments should be reassembled.

3. Acknowledgments (ACKs)

  • After receiving a segment, the receiver sends an acknowledgment (ACK) back to the sender, confirming the receipt of data.

  • The ACK includes the next expected sequence number, so the sender knows that all data up to that point has been successfully received.

  • If the sender doesn’t receive an ACK within a certain time, it will retransmit the data.

4. Error Checking (Checksums)

  • Each TCP segment includes a checksum, which helps detect errors during transmission.

  • The receiver checks the checksum to verify the integrity of the received data. If the checksum is incorrect, the segment is discarded, and the receiver requests a retransmission.

5. Flow Control (Windowing)

  • Flow control is used to prevent the sender from overwhelming the receiver with too much data at once.

  • TCP uses a mechanism called the sliding window, where the receiver advertises how much data it can accept at once, and the sender adjusts its transmission rate accordingly.

6. Congestion Control

  • TCP also uses congestion control algorithms to prevent network congestion and ensure that data is transmitted at a rate suitable for the current network conditions.

  • These algorithms adjust the rate of data transmission based on feedback from the network.

7. In-order Delivery

  • TCP guarantees that data is received in the same order it was sent, even if packets arrive out of order due to network routing. This is done using the sequence numbers and reordering the segments at the receiver’s end.

8. Connection Termination

  • Once the communication is complete, the connection is terminated using a 4-way handshake (FIN → ACK → FIN → ACK).

  • This ensures that both the sender and receiver agree that the connection has ended, and all remaining data is transmitted.