TCP 3 Way Handshake: Reliable Connection Establishment

fundamentals-of-networking-titled-Sanchit-Gurukul
06/07/2021 •

TCP 3 Way Handshake

The TCP 3 way handshake is a process used to establish a reliable connection between a client and a server. It ensures that both parties are ready to communicate and can reliably exchange data. The handshake involves three steps: SYN, SYN-ACK, and ACK

Steps of the TCP 3 way handshake

  1. SYN (Synchronize):
    • The client initiates the connection by sending a SYN packet to the server. This packet contains a randomly chosen sequence number (Seq = x).
    • This step indicates that the client wants to establish a connection and synchronizes the sequence numbers.
  2. SYN-ACK (Synchronize-Acknowledge):
    • The server responds with a SYN-ACK packet. This packet acknowledges the client’s SYN packet (Ack = x + 1) and includes its own randomly chosen sequence number (Seq = y).
    • This step acknowledges the receipt of the client’s request and provides its own sequence number for synchronization.
  3. ACK (Acknowledge):
    • The client sends an ACK packet back to the server. This packet acknowledges the server’s SYN-ACK packet (Ack = y + 1) and completes the handshake.
    • The connection is now established, and data transfer can begin.

Example Sequence TCP 3 way handshake

tcp 3 way handshake
  1. Client to Server: SYN, Seq = x
  2. Server to Client: SYN-ACK, Seq = y, Ack = x + 1
  3. Client to Server: ACK, Seq = x + 1, Ack = y + 1

Benefits of the TCP 3 way handshake

  1. Reliability: Ensures both parties are ready and synchronized for communication, reducing the risk of data loss.
  2. Synchronization: Establishes initial sequence numbers for both parties, crucial for ordered data transmission.
  3. Connection Confirmation: Both client and server confirm their willingness and ability to communicate, preventing half-open connections.
  4. Resource Allocation: Allows both parties to allocate necessary resources (e.g., buffers, processing power) for the upcoming data transfer.

TCP 3 way handshake in real world

Client side

Server Side

TCP 4-Way Connection Termination

Closing a TCP connection involves a 4-way handshake to ensure that both parties properly terminate the connection and no data is lost. The process involves four steps: FIN, ACK, FIN, and ACK.

Steps of the 4-Way Connection Termination:

  1. FIN (Finish):
    • The party that wants to close the connection (usually the client) sends a FIN packet to the other party. This packet indicates that the sender has finished sending data (Seq = u).
    • The sender enters the FIN-WAIT-1 state.
  2. ACK (Acknowledge):
    • The receiver acknowledges the FIN packet with an ACK packet (Ack = u + 1). The receiver enters the CLOSE-WAIT state.
    • The sender enters the FIN-WAIT-2 state.
  3. FIN (Finish):
    • The receiver sends its own FIN packet when it is ready to close the connection (Seq = v). This indicates that the receiver has finished sending data.
    • The receiver enters the LAST-ACK state.
  4. ACK (Acknowledge):
    • The sender acknowledges the receiver’s FIN packet with an ACK packet (Ack = v + 1). The sender enters the TIME-WAIT state and waits for a specified period before closing the connection completely.
    • The receiver closes the connection and enters the CLOSED state.

Example Sequence TCP 4 Way Termination

  1. Client to Server: FIN, Seq = u
  2. Server to Client: ACK, Ack = u + 1
  3. Server to Client: FIN, Seq = v
  4. Client to Server: ACK, Ack = v + 1

Summary

The TCP 3-way handshake is a fundamental process for establishing a reliable connection between a client and a server. By exchanging SYN, SYN-ACK, and ACK packets, both parties confirm their readiness to communicate and synchronize their sequence numbers, ensuring reliable data transmission.

Benefits Recap:

  • Reliability: Ensures a reliable communication channel.
  • Synchronization: Sets up initial sequence numbers for ordered data transfer.
  • Connection Confirmation: Prevents half-open connections.
  • Resource Allocation: Allows allocation of necessary resources for data transfer.

The 4-way handshake is used to gracefully close a TCP connection. By exchanging FIN and ACK packets, both parties confirm that they have finished sending data and can safely release resources associated with the connection.

Key Steps for Closing a Connection:

  1. FIN from Initiator: Initiates the termination process.
  2. ACK from Receiver: Acknowledges the FIN packet.
  3. FIN from Receiver: Indicates that the receiver is ready to close the connection.
  4. ACK from Initiator: Final acknowledgment, ensuring a clean termination.

The 4-way handshake ensures that no data is lost during the termination process and that both parties properly close the connection. This meticulous process helps maintain the reliability and integrity of TCP connections, preventing issues like data loss or resource leaks.

Conclusion

TCP’s 3-way handshake is crucial for establishing a reliable and synchronized communication channel between a client and a server. Its careful process ensures both parties are ready to exchange data, setting the stage for reliable and ordered data transmission. Meanwhile, the 4-way handshake provides a structured method for terminating connections, ensuring all data is transmitted and acknowledged before closing. These mechanisms highlight TCP’s emphasis on reliability and data integrity, making it a cornerstone of modern network communications.

https://datatracker.ietf.org/doc/html/rfc9293

https://sanchitgurukul.com/tutorials-cat

Disclaimer: This article may contain information that was accurate at the time of writing but could be outdated now. Please verify details with the latest vendor advisories or contact us at admin@sanchitgurukul.com.

Discover more from

Subscribe now to keep reading and get access to the full archive.

Continue reading