Skip to main content

Command Palette

Search for a command to run...

TCP Working: 3-Way Handshake & Reliable Communication

Published
4 min read
TCP Working: 3-Way Handshake & Reliable Communication
S
2nd year CSE student. MERN Stack Developer. Building scalable web apps and documenting the journey." (Keep it energetic and focused on pure software engineering—no need for the typical "fueled by caffeine" developer cliches, staying hydrated and focused works best!)

Ever wondered why a WhatsApp message doesn't just disappear into thin air, or why a website doesn't load half-jumbled? That’s because of TCP (Transmission Control Protocol).

Think of TCP as a very "strict" post office. If you send a letter through them, they don't just drop it and leave; they call you to say it arrived, and if it gets lost, they go back and send it again.

Why do we even need TCP?

Imagine if the internet had no rules. You’d send a "Hello World" message, and the server might receive "World Hello" or maybe nothing at all.

  • Packet Loss: Data getting lost in the "noise" of the internet.

  • Wrong Order: The end of your file arriving before the start.

  • Corruption: Data getting "bruised" or changed during the trip.

TCP was built to solve all of this. It’s the "Reliability King."

The TCP 3-Way Handshake: "Hello, Are You There?"

Before any actual data is sent, TCP sets up a special "conversation" to make sure both sides are ready and listening. This is called the 3-Way Handshake, and it's like two people agreeing to talk before starting a serious discussion.

Let's use an analogy: You (the Client) want to talk to Your Friend (the Server).

  1. SYN (Synchronize) - You say "Hello?"

    • Client to Server: "Hey, I want to talk! Are you ready to receive my messages?"

    • Technical Bit: Your computer sends a SYN packet. It's asking to establish a connection.

  2. SYN-ACK (Synchronize-Acknowledge) - Your Friend says "Yes, I am!"

    • Server to Client: "Yes, I hear you! I'm ready, and I'm also saying hello back to confirm."

    • Technical Bit: The server sends a SYN-ACK packet. This acknowledges your SYN and also tries to set up its own connection to you.

  3. ACK (Acknowledge) - You say "Got it, Let's talk!"

    • Client to Server: "Great! I heard you loud and clear. We're good to go!"

    • Technical Bit: Your computer sends an ACK packet. This confirms the connection is now fully established.

And just like that, a reliable connection is ready for data transfer!

Diagram of the TCP 3-Way Handshake showing the interaction between a client and a server. The process includes three steps: SYN from client to server, SYN-ACK from server to client, and ACK from client to server. Details of each step are displayed to the right. Connection is established after these exchanges.

How Data Transfer Works in TCP: The Postal Service Analogy

Once the handshake is done, TCP starts sending your actual data. But it doesn't just send one huge chunk.

  1. Breaking It Down: TCP chops your data into smaller "segments" (like putting a big letter into many small, numbered envelopes).

  2. Sequence Numbers: Each segment gets a unique "sequence number." This tells the receiver the correct order of the envelopes.

  3. Acknowledgements (ACKs): For every segment received, the other side sends an ACK message back, saying, "Got segment #5! Send #6 next!"

Diagram showing how TCP tracks data using sequence and acknowledgment numbers. It depicts data segments sent from a sender to a receiver, with acknowledgment responses. Sequence starts at 100, with acknowledgments at 150 and 200. A buffer holds sequence 230 as TCP waits for missing sequence 200.

How TCP Stays Reliable: Never Lose a Letter!

  • Order Guaranteed: Because of sequence numbers, even if segments arrive out of order, TCP puts them back correctly.

  • Error Checking: TCP checks if any data got corrupted. If it does, it asks for that specific segment again.

  • Retransmission (Lost Segments): If an ACK isn't received after a certain time (meaning the segment was lost), TCP simply resends that segment. It won't move on until it's sure the other side got it.

This whole process is like a meticulous post office: every letter is numbered, confirmed, and re-sent if lost, ensuring your message arrives exactly as intended.

Diagram showing how TCP fixes lost data in four parts: 1) Normal Flow - Packet 1 is sent and acknowledged. 2) Packet Loss - Packet 2 is lost in the network. 3) Detection - Timeout occurs, no confirmation received. 4) Retransmission - Packet 2 is resent, acknowledged successfully.

Closing the TCP Connection: "Goodbye!"

When you're done talking (e.g., you close a webpage), TCP doesn't just hang up. It gracefully closes the connection, just like the handshake, but in reverse.

  1. FIN (Finish) - You say "I'm done sending!"

  2. ACK (Acknowledge) - Friend says "Got it!"

  3. FIN (Finish) - Friend says "I'm also done sending!"

  4. ACK (Acknowledge) - You say "Got that too!"

This 4-step process ensures both sides agree that no more data is coming, preventing any sudden cut-offs.

Illustration titled "The Life of a TCP Connection: Start to Finish" shows three steps of a TCP connection: a handshake, reliable data transfer with data segments, and a four-way termination. Arrows depict the data exchange between a client (laptop) and a server.

More from this blog