Skip to main content

Command Palette

Search for a command to run...

TCP Working: 3-Way Handshake & Reliable Communication

Updated
3 min read
TCP Working: 3-Way Handshake & Reliable Communication
R
I’m a BCA student and a full-stack developer passionate about building scalable web applications. I enjoy working with modern technologies and sharing my learning through blogs.

What is TCP ?

Transmission Control Protocol (TCP) is a core, connection-oriented networking protocol that ensures reliable, ordered, and error-checked delivery of data between applications on the internet. It is essential because it manages packet loss, handles congestion, and guarantees that data arrives complete and in the correct sequence, forming the foundation for web browsing, email, and file transfers.

Problems TCP is designed to solve

TCP (Transmission Control Protocol) is designed to solve the inherent unreliability of IP networks by ensuring guaranteed data delivery, ordered packet sequencing, and error detection. It manages, controls, and corrects network communication issues by preventing data loss, handling congestion, and ensuring orderly, flow-controlled data transfer.

What is the TCP 3-Way Handshake :-

The TCP 3-Way Handshake is a process used by the Transmission Control Protocol (TCP) to establish a reliable connection between a client and a server before data transfer. It ensures that both sides are synchronized and ready to communicate

1. SYN (Synchronize) - Client to Server

The client initiates the connection by sending a TCP packet with the SYN flag set to 1, signaling a request to start a session.

  • Action: Client selects an Initial Sequence Number (ISN), let's call it xx𝑥, and sends it to the server.

  • Packet Contents: SYN = 1, ACK = 0, Sequence Number = xx𝑥.

  • Purpose: To inform the server that the client wants to establish a connection and to synchronize the starting sequence number.

  • State: The client moves from CLOSED to SYN-SENT.

2. SYN-ACK (Synchronize-Acknowledgment) - Server to Client

Upon receiving the SYN packet, the server acknowledges it and replies with its own request to establish a connection.

  • Action: The server sets the SYN flag to 1 (to establish its side) and the ACK flag to 1 (to acknowledge the client's packet). It sends its own random initial sequence number (yy𝑦) and sets the acknowledgment number to x+1x plus 1𝑥+1.

  • Packet Contents: SYN = 1, ACK = 1, Sequence Number = yy𝑦, Acknowledgment Number = x+1x plus 1𝑥+1.

  • Purpose: To acknowledge the client’s request and inform the client of the server's starting sequence number.

  • State: The server moves to SYN-RECEIVED.

3. ACK (Acknowledgment) - Client to Server

The client receives the SYN-ACK packet and sends a final acknowledgment back to the server.

  • Action: The client acknowledges the server's sequence number (yy𝑦) by setting the acknowledgement number to y+1y plus 1𝑦+1. The ACK flag is set to 1.

  • Packet Contents: ACK = 1, Sequence Number = x+1x plus 1𝑥+1, Acknowledgment Number = y+1y plus 1𝑦+1.

  • Purpose: To confirm receipt of the server's SYN-ACK and finalize the connection.

  • State: Both client and server move to the ESTABLISHED state.

How data transfer works in TCP ?

TCP (Transmission Control Protocol) facilitates reliable data transfer by establishing a connection via a three-way handshake (SYN, SYN-ACK, ACK), breaking data into ordered segments, and ensuring error-free delivery through acknowledgments and retransmissions. It manages flow control, adjusts to network congestion, and closes the connection with a four-way handshake.

TCP ensures reliable, ordered, and correct data delivery through a connection-oriented approach using sequence numbers, acknowledgments (ACKs), checksums, and retransmission mechanisms

A TCP connection is typically closed using a four-way handshake, where each side of the connection terminates independently to ensure all data is delivered. Either the client or server initiates the process by sending a FIN (Finish) packet, which is acknowledged (ACK) by the other side, followed by a similar exchange to close the reverse direction.