Skip to main content

Command Palette

Search for a command to run...

TCP Working: 3-Way Handshake & Reliable Communication

How computers talk reliably, step by step

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

I build clean and simple web experiences and learn something new every day.

Whenever we open a website, send a message, or download a file, data is constantly moving from one computer to another.
But have you ever wondered how this data reaches the correct destination safely and in the right order?

That’s where TCP (Transmission Control Protocol) comes into the picture.

In this blog, I’ll explain how TCP works, why it is important, and how the famous 3-way handshake helps in establishing a reliable connection in a simple and relatable way.


What is TCP and why do we need it?

TCP is a connection-oriented protocol that makes sure data is delivered reliably between two devices.

The internet is not perfect:

  • Data packets can get lost

  • They can arrive in the wrong order

  • Some packets might even get duplicated

TCP handles all of this for us so that applications don’t have to worry about it.

That’s why TCP is used in:

  • Web browsing (HTTP / HTTPS)

  • Emails

  • File transfers

  • Online payments

Basically, anywhere accuracy matters, TCP is used.


The problem TCP solves

Imagine you are sending a long handwritten letter page by page:

  • Some pages might get lost

  • Some might arrive late

  • Some might reach in the wrong order

Now imagine numbering each page and asking the receiver to confirm which pages they received.
If any page is missing, you resend it.

That is exactly how TCP works, but with data packets.


Before sending data, a connection must be created

TCP does not start sending data immediately.
First, it makes sure that both the sender and receiver are ready to communicate.

This process is called the TCP 3-Way Handshake.


TCP 3-Way Handshake (step by step)

The 3-way handshake is simply a conversation between client and server to start a connection.

Step 1: SYN (Client → Server)

The client sends a message saying:

Hey, I want to connect with you.

This message is called SYN (Synchronize) and it also includes an initial sequence number.

At this point, the client is just checking if the server is available.


Step 2: SYN-ACK (Server → Client)

If the server is ready, it replies with:

  • SYN: “I’m ready too”

  • ACK: “I received your request”

This tells the client:

Yes, I’m here and I acknowledge your request.


Step 3: ACK (Client → Server)

Finally, the client sends an ACK back to the server confirming:

I received your response. Let’s start communicating.

After this step, the connection is successfully established.

Now both sides trust each other and are ready to exchange data.


Why is it called a 3-way handshake?

Because three messages are exchanged to make sure:

  • The client is reachable

  • The server is reachable

  • Both are synchronized

This avoids confusion and prevents half-open or fake connections.


How data is transferred using TCP

Once the connection is established:

  • Data is divided into small packets

  • Each packet has a sequence number

  • The receiver sends ACKs for received packets

  • Lost packets are automatically resent

So even if:

  • A packet is lost

  • Packets arrive out of order

TCP fixes everything behind the scenes.


Sequence numbers and acknowledgments (in simple words)

  • Sequence numbers help TCP arrange packets in the correct order

  • ACKs (Acknowledgements) tell the sender which data has been received

If the sender doesn’t receive an ACK:

  • It assumes the packet is lost

  • It resends the packet

This is how TCP guarantees reliable delivery.


What if a packet is lost?

No panic
TCP handles it automatically.

  • The receiver notices a missing sequence number

  • The sender doesn’t get an ACK

  • The sender resends the missing packet

The user never even notices that something went wrong.


How does TCP close a connection?

Just like starting a connection, TCP also ends it properly.

  • One side sends a FIN message saying “I’m done”

  • The other side sends ACK

  • Then it sends its own FIN

  • Final ACK is sent and the connection is closed

This ensures that no data is left hanging.


Why TCP is reliable but slightly slower

TCP takes extra steps:

  • Acknowledgements

  • Retransmissions

  • Flow control

Because of this, TCP is slower than UDP.
But in return, it gives accuracy and reliability, which is more important for most applications.


Final thoughts

TCP might look complex at first, but its goal is very simple:

Make sure data reaches the right place, in the right order, without loss.

The 3-way handshake builds trust, sequence numbers maintain order, and acknowledgements ensure delivery.

Once you understand TCP, many internet concepts start making a lot more sense.

More from this blog

C

codeXninjaDev

54 posts

I build clean and simple web experiences and learn something new every day.