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

Imagine shouting instructions across a noisy street—some words are missed, some are heard twice, and some arrive in the wrong order. If the same thing happened with bank transactions, files, or web pages, the results would be disastrous.

This is exactly what would happen on the internet without rules, because data travels through many networks and can get lost, duplicated, delayed, or reordered along the way.

To prevent this chaos, the internet needs a clear rulebook that says how to send data carefully, check whether it arrived, and fix problems if it didn’t—and that rulebook is called TCP.

What is TCP and why it is needed

TCP (Transmission Control Protocol) is a communication protocol designed to provide reliable data transfer between two devices.

At a very high level, TCP ensures:

  • Data reaches the destination

  • Data arrives in the correct order

  • Missing data is detected and re-sent

Without TCP, most internet applications would break.

Problems TCP Is Designed to Solve :

  • Packet loss – some data disappears on the way

  • Out-of-order delivery – packet 3 arrives before packet 1

  • Duplicate packets – same data arrives twice

  • Unreliable connections – sender doesn’t know if receiver is ready

What Is the TCP 3-Way Handshake?

Before sending actual data, TCP first sets up a connection between the sender and receiver. This setup process is called the 3-Way Handshake.

Think of it like starting a phone call :

  1. “Can you hear me?”

  2. “Yes, I can hear you.”

  3. “Great, let’s talk.”

Only after this does real communication begin.

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

Step 1: SYN (Synchronize)

The client device initiating the data transfer sends a sequence number (SYN) to the server. It tells the server the number that the data packet transfer should begin with.

Client → Server

“Hi, I want to start a connection.”

This message is called SYN.


Step 2: SYN-ACK (Synchronize + Acknowledge)

The server acknowledges the client SYN and sends its own SYN number. This step is often referred to as SYN-ACK (SYN acknowledgement).

Server → Client

“I got your request, and I’m ready too.”

This response is SYN + ACK.


Step 3: ACK (Acknowledge)

The client then acknowledges (ACK) the server’s SYN-ACK, which forms a direct connection and begins the data transfer.

Client → Server

“Perfect. Let’s start sending data.”

Connection is now established.

How Data Transfer Works in TCP

Once the connection is open:

  • Data is broken into small numbered pieces

  • Each piece is sent separately

  • The receiver sends back acknowledgements

If something goes wrong:

  • Missing data is detected

  • Sender re-sends only what’s missing

How TCP Ensures Reliability, Order, and Correctness

TCP is designed so that applications don’t have to worry about lost, mixed, or corrupted data. It quietly handles these problems in the background. Here’s how it does that—at a high level.

1. Reliability: Making Sure Data Arrives

TCP ensures that data actually reaches the destination.

  • Data is split into small pieces (segments)

  • The receiver sends back ACKs (acknowledgements) for received data

  • If an ACK is not received in time, TCP re-sends the missing data

👉 Result: even if the network drops packets, TCP recovers automatically.


2. Order: Keeping Data in the Right Sequence

On the internet, data packets can arrive out of order.

TCP fixes this using sequence numbers:

  • Every data segment is numbered

  • The receiver uses these numbers to reassemble data correctly

  • Out-of-order segments are stored and rearranged

👉 Result: the application receives data in the exact order it was sent.


3. Correctness: Detecting Errors

Sometimes data arrives corrupted due to network issues.

TCP handles this by:

  • Checking for errors in received data

  • Discarding corrupted segments

  • Asking the sender to re-send correct data

How a TCP Connection Is Closed

When two devices finish communicating, TCP does not just stop suddenly. It follows polite rules to make sure no data is cut off and both sides agree that the connection is ending.

TCP uses two special messages:

  • FIN (Finish) – “I’m done sending data.”

  • ACK (Acknowledge) – “I received your message.”

Just like starting a connection is polite, ending it cleanly matters too. TCP closes connections using FIN and ACK messages.

More from this blog