Skip to main content

Command Palette

Search for a command to run...

#9 TCP Working: 3-Way Handshake & Reliable Communication

Updated
2 min read
P
Since I am developer. My blogs are related to Tech and also blogs write during web dev cohort by chai code.

When you send a message over the internet, it doesn't travel as one solid piece. It is broken into tiny "packets." Without rules, these packets could arrive out of order, get lost, or become corrupted.

TCP (Transmission Control Protocol) is the set of rules that ensures your data arrives exactly as it was sent. It is the "Contract" of the internet.

1. The 3-Way Handshake: Starting the Conversation

Before any data (like your React code or a PHP script) is sent, the sender (Client) and receiver (Server) must agree to talk. They do this through a 3-Way Handshake.

Think of it like two people meeting for the first time:

  1. SYN (Synchronize): The Client says, "I want to talk. Here is my starting sequence number."

  2. SYN-ACK (Synchronize-Acknowledge): The Server replies, "I heard you! I'm ready too. Here is my sequence number, and I've noted yours."

  3. ACK (Acknowledge): The Client says, "Got it! Let’s start sending data."

2. Ensuring Reliability: The "Checklist"

Once the connection is open, TCP uses Sequence Numbers and Acknowledgements (ACKs) to track every bit of data.

  • Ordering: Every packet is numbered (1, 2, 3...). If packet #3 arrives before packet #2, TCP waits and puts them back in the right order before showing them to your application.

  • Correctness: TCP performs a "checksum" (a quick math test) on every packet to ensure the data wasn't corrupted during transit.

  • Handling Packet Loss: If the Server receives packet #1 and #3 but #2 is missing, it won't "ACK" #2. The Client realizes this and automatically retransmits the missing packet.

3. Flow and Congestion Control

TCP is smart enough to know if it's sending data too fast.

  • If the Server's "inbox" is getting full, TCP tells the Client to slow down.

  • If the network itself is crowded (congestion), TCP reduces the speed of delivery to prevent the connection from crashing.

4. Closing the Connection: The 4-Way Wave

Closing a connection is just as orderly as opening one. It requires a 4-step process to ensure both sides have finished sending all their data.

  1. FIN: Client says, "I'm done sending data."

  2. ACK: Server says, "I acknowledge you're done. Let me finish my last bit of work."

  3. FIN: Server says, "I'm done too."

  4. ACK: Client says, "Okay, goodbye!"

5. Why this matters for your work

Every "handshake" takes time (round-trip time). This is why a website might feel slow even if the internet speed is high—it's the constant "back-and-forth" of TCP making sure everything is perfect.