Skip to main content

Command Palette

Search for a command to run...

TCP Working: 3-Way Handshake & Reliable Communication

Published
3 min read
TCP Working: 3-Way Handshake & Reliable Communication

What Is TCP and Why It Needed

TCP (Transmission Control Protocol) is a connection-oriented transport protocol used on the Internet to send data reliably between two applications. It sits at the transport layer above the IP layer. Because IP can lose, reorder, or duplicate packets, TCP adds rules so that both sides can detect errors, recover lost packets, and reassemble data correctly at the receiver.

Without TCP or a similar protocol, even simple message delivery over an unreliable network could result in missing or corrupted files, confusing errors, or out-of-order information arriving at the destination.


What Problems TCP Solves

TCP is designed to handle common network issues:

  • Lost packets — if a packet doesn’t arrive, TCP requests it again.

  • Out-of-order packets — packets may arrive in a different sequence than sent; TCP reorders them correctly.

  • Duplicate packets — TCP detects and removes duplicates.

  • Flow and congestion control — TCP adjusts data flow based on network and receiver capacity.


What Is the 3-Way Handshake?

Before any application data is sent, TCP sets up a reliable connection using a 3-way handshake: a conversation between the client and server to agree that both are ready and synchronized.

A Simple Analogy

Imagine two people meeting by phone:

  1. Caller: “Are you there?”

  2. Receiver: “Yes — and I’m ready. Are you there?

  3. Caller: “Yes, I’m ready — let’s talk!”

Only after this mutual confirmation does the real conversation start. TCP’s handshake works the same way.


Step-by-Step: SYN, SYN-ACK, ACK

Each step uses TCP flags to communicate intentions:

  1. SYN (Synchronize):
    The client sends a packet with the SYN flag to the server — this means “I want to open a connection.”

  2. SYN-ACK (Synchronize & Acknowledge):
    The server responds with a packet that has both SYN and ACK flags set:

    • ACK confirms it received the client’s SYN.

    • SYN tells the client “I agree, and I also want to open a connection to you.”

  3. ACK (Acknowledge):
    The client replies with an ACK packet back to the server — now both sides know the other is ready and synchronized.

Once this exchange completes, the TCP connection is established and ready for real data transfer.


How TCP Ensures Reliable, Ordered Communication

After the handshake:

  • TCP uses sequence numbers to track each byte of data, so the receiver can reassemble data in order.

  • The receiver sends ACKs for received data; if a packet is lost, the sender retransmits it.

  • If network conditions change, TCP adjusts how fast packets are sent to avoid congestion.

This combination of synchronized sequence numbers and acknowledgments is what makes TCP reliable, even over unpredictable networks.


Closing a TCP Connection

When communication is done, TCP doesn’t just stop — it closes the connection gracefully using a similar handshake process:

  • One endpoint sends FIN (finish) to indicate it has no more to send.

  • The other acknowledges with ACK.

  • Often each side goes through this four-step procedure (FIN → ACK → FIN → ACK) to allow both directions to finish independently.

This ensures that all data has been fully transmitted and acknowledged before the connection truly ends.


Summary

  • TCP provides reliability and correctness on top of the basic, unreliable IP network by detecting errors and retransmitting missing data.

  • The 3-way handshake is the foundation of TCP’s connection-oriented behavior — it makes sure both ends are ready and synchronized before sending real data.

  • Sequence numbers and ACKs allow TCP to maintain order, detect loss, and manage flow.

  • Connection close uses a similar handshake mechanism to ensure a clean end.