Skip to main content

Command Palette

Search for a command to run...

TCP Working: 3-Way Handshake & Reliable Communication

Published
5 min read

Imagine you are trying to tell a friend a long, complicated story in a crowded, noisy room. If you just started shouting the story from the beginning without checking if they were listening, they might miss the first half. If someone walks between you, they might miss a crucial sentence. By the end, they might hear the ending before the middle.

Without rules, the internet works the same way. Data can get lost, arrive out of order, or get corrupted.

This is where TCP (Transmission Control Protocol) comes in. It is the internet's way of ensuring a polite, reliable conversation where every word is heard, understood, and put in the right order.


What is TCP and Why Do We Need It?

TCP is a fundamental communication protocol that computers use to talk to each other. It sits on top of IP (Internet Protocol), which handles the addressing (finding where to go), while TCP handles the conversation (ensuring the data arrives correctly).

The Problems TCP Solves

Without TCP, the internet is just a series of "best-effort" attempts. If we didn't use TCP for things like loading webpages or sending emails, you might face these issues:

  • Packet Loss: Parts of your email go missing.

  • Out-of-Order Data: You receive page 10 of a document before page 1.

  • Corruption: The data arrives, but it looks like gibberish.

TCP is designed to solve all three. It guarantees that if you send data, it will arrive, it will be correct, and it will be in the right order.


The 3-Way Handshake: "Hello, Can You Hear Me?"

Before TCP sends any actual data (like an image or an email), it must establish a connection. It does this through a process called the 3-Way Handshake.

Think of it like starting a polite phone call:

  1. You: "Hello, are you there?"

  2. Friend: "Yes, I'm here. Can you hear me?"

  3. You: "Yes I can. Let's talk."

In technical terms, the computers use special "flags" or markers in their messages called SYN (Synchronize) and ACK (Acknowledge).

Step-by-Step Working

  1. Step 1: SYN (The Request) The Client (your computer) wants to talk to a Server (e.g., Google). It sends a packet with the SYN flag turned on. This effectively says, "I want to synchronize a connection with you. My sequence number is X."

  2. Step 2: SYN-ACK (The Agreement) The Server receives the request. It must acknowledge that it heard the client, but it also needs to start its own conversation. It sends back a packet with both SYN and ACK flags. This says, "I acknowledge your request (ACK), and I also want to synchronize with you (SYN)."

  3. Step 3: ACK (The Confirmation) The Client receives the server's response. It sends one final ACK packet back. This says, "I acknowledge that you are ready. The connection is established."

Once this third step is complete, the connection is "ESTABLISHED," and real data can start flowing.


How TCP Ensures Reliability

Once the handshake is done, how does TCP ensure the "story" is told correctly? It uses two clever mechanisms: Sequence Numbers and Acknowledgments.

1. Putting Things in Order (Sequence Numbers)

When you send a large file, TCP breaks it down into small chunks called "packets." It assigns a Sequence Number to every byte of data.

Imagine you are mailing a 100-page book to a friend, but you have to mail each page in a separate envelope. You would number the pages 1, 2, 3... so your friend can put them back in order, even if the mailman delivers page 50 before page 2.

2. Confirming Receipt (Acknowledgments)

Every time the receiver gets a packet, it sends an ACK back to the sender. This ACK tells the sender exactly how much data has been received successfully.

  • If the Sender sends Packet #1: It waits for an "ACK #1" from the Receiver.

  • If the Sender gets the ACK: It knows it's safe to send Packet #2.

3. Handling Lost Data (Retransmission)

What if a packet gets lost in the "noisy room" of the internet?

  • If the Sender sends a packet but does not receive an ACK within a specific time limit (timeout), it assumes the packet was lost.

  • The Sender effectively says, "I didn't hear you say you got that. I'll say it again."

  • It retransmits the missing packet automatically. This ensures zero data loss.


Closing the Connection: The Polite Goodbye

Just as TCP is polite when starting a conversation, it is polite when ending one. It doesn't just hang up; it ensures both sides are done talking. This is usually a 4-step process involving FIN (Finish) and ACK flags.

  1. Client: Sends a FIN packet ("I am done sending data.").

  2. Server: Sends an ACK ("I received your request to stop.").

  3. Server: Sends its own FIN packet ("I am also done sending data now.").

  4. Client: Sends a final ACK ("Understood. Goodbye.").

After this, the resources are released, and the connection is officially closed.


Conclusion

TCP is the invisible workhorse of the internet. By using the 3-Way Handshake, it ensures the other party is listening. By using Sequence Numbers and ACKs, it ensures your data arrives perfectly, every single time. It sacrifices a tiny bit of speed for total reliability—a trade-off that makes the modern web possible.