Skip to main content

Command Palette

Search for a command to run...

TCP Working: 3-Way Handshake & Reliable Communication

Published
7 min readView as Markdown
TCP Working: 3-Way Handshake & Reliable Communication
R
the topics and concepts which i learn and get more fascinated i write about them here...

In the previous article, we spoke about TCP vs UDP and why the internet needs rules to transfer data safely. Now lets move into TCP itself and understand how TCP actually works behind the scenes.

In this blog we will learn why TCP exists, why it is needed, what problems it solves and how the famous 3-way handshake works.

What happens when data is sent without rules?

As we know when many devices are in a network and communicate with each other they follow some certain rules for sending data, what if we dont follow these rules, imagine sending long messages to a friend by shouting works randomly, in which some works are heard some goes missing while some arrive out of order and the message becomes meaningless. This is exactly what happens on the internet if data is sent without coordination, Packets may get lost, they might arrive out of order or they may get duplicated. So we need a system that says, “ive received this message, ive not received the middle part please send again etc. This is exactly what TCP does.

What is TCP and why it is needed?

Another important reason TCP is needed is trust between machines. On the internet, our laptop talks to servers it has never met before so we cant trust those servers so TCP creates a temporary but trusted relationship where both sides agree on How data will be sent then how fast it should be and how to recover if something goes wrong. Without TCP every application would have to write these rules on their own making internet unreliable and not trustworthy.

TCP(Transmission Control Protocol) is a transport layer protocol that ensures:

  • Reliable data delivery

  • Correct ordering of data

  • Error detection and recovery

TCP is used when accuracy matter more that speed. Like i want a data packet to be received fully with no missing packets rather than delivering it early. We can think of TCP as a amazon delivery where you order 4-5 products and you have to wait for days to get all the products at same time and day.

TCP comes at 4 layer of OSI model. OSI model is nothing but a conceptual model created by International Organization for Standardization which enables different communication systems to communicate using standard protocols. It consists of 7 layers each stacked upon the last.

Each layer of OSI model has a specific job and communicates with the layer above and below.

Problems TCP is Designed to Solve

TCP is created to solve some important problems:

  1. Packet Loss

    Packets can disappear due to network traffic. TCP detects missing packets and resends them

  2. Out of Order

    Packets while traveling from one system to other might take different routes to reach target destination. TCP helps in reordering the packets.

  3. Duplicate Packets

    During the journey from one system of another some packets may arrive twice. TCP helps them to remove duplicates.

  4. Unreliable Communication

    When a sender sends the packets to the receiver, sender has no idea that the data packet has received to the respective receiver. TCP uses acknowledgements so that he gets to know that the packet has received.

The TCP 3-way Handshake

Whenever we meet someone we greet them by shaking hands and then start our conversation with the person thats the same think how TCP handshake works. We can think of handshake as a safety check before starting journey. TCP has no idea that the network is stable it asks questions like,

  • Are you available?

  • Can you receive the data?

  • Can I receive data back?

When the answer for all these questions is received then TCP allows data to flow. Before any real data is sent, TCP first establishes a connection. This process is called 3-way handshake. This is done to confirm both sides are ready and to avoid sending data blindly.

3-way Handshake: In layman language

Imagine a phone call or we are all on google meet or a video call, You start the conversion by saying hello can you hear me? the person on other end replies yes i can! can you hear me? and then you reply yes and later the conversation begins ahead.

Imagine this in a client server model:

  • Client: “Hey, Can you hear me?”

  • Server: “Yes I can hear you! Can you hear me?”

  • Client: “Yes, I can lets start the conversation!”

Only after this confirmation the actual conversation begins, this is nothing but a TCP 3 way handshake.

How TCP 3-way Handshake Works

Step 1: SYN (Synchronize)

The client sends a SYN packet:

  • “I want to connect”

  • Includes an initial sequence number

This means client wants to start a TCP connection

Step 2: SYN-ACK (Synchronize + Acknowledge)

The server responds with SYN-ACK:

  • Acknowledges client’s request

  • Sends its own sequence number

This is the server has heard the client and server is ready for connection

Step 3: ACK (Acknowledge)

The client send a ACK back:

  • Acknowledges server’s sequence number

This means connection is established and then data can be sent. Now TCP connection is Live.

What are sequence numbers?

When a data packet is sent from server to client TCP breaks the data packet into chunks and assigns each chunk a sequence number. This is done to Track the Order, to detect missing data.

We can take a example of a book, now tear the pages and send one by one to the client. Here page number means sequence number. Now in this in Page 3 is missing TCP knows exactly what to ask for and search for Page 3. TCP then combines all the pages accordingly wrt to page number and transfers the book to the client.

How Data Transfer works in TCP

Once the 3-way handshake is complete, TCP then switches form setup mode to data transfer mode. At this stage, TCP behaves as a strict messenger:

  • Sends a limited amount of data

  • Waits for confirmation

  • Adjusts speed based on network conditions

This is why TCP connections may slow down when the network is bad instead of failure.

Once the connection is established:

  1. Sender sends data with sequence numbers

  2. Receiver sends ACKs for received data

  3. Sender moves forward only after confirmation

TCP uses a send → acknowledge → continue loop

How TCP Ensures Reliability

Reliability in TCP cannot be achieved by a single feature, but by multiple small guarantees working together.

Even if the network behaves weird, TCP stays calm ensuring the client or application receives clean and complete data. TCP guarantees reliability using multiple mechanisms which are:

  1. Acknowledgements (ACKs)

    Receiver confirms received data.

  2. Retransmission

    If ACK is not received, data is sent again

  3. Sequence number

    Ensures correct order of data packets

  4. Flow Control

    Prevents overwhelming the receiver

How TCP Handles Packet Loss

If a packet is lost:

  • Receiver notices a missing sequence number.

  • Receiver does not acknowledge that packet

  • Sender waits for a timeout

  • Sender sends the missing packet again

So no data is silently lost

How a TCP connection is closed

TCP treats connection closing very serious as connection opening because:

  • To ensure no data is accidentally dropped

  • To allow both sides to finish pending work

  • To avoid half-open connections

Ending a TCP connection is also controlled not just abruptly ended.

Connection Termination steps

  1. One side send the FIN (Finish Flag)

  2. Other side send a ACK

  3. Then other side sends its own FIN

  4. Later First side sends a final ACK

Only then the connection is closed safely

Example:

Ending a Phone call:

  • “I’m done”

  • “Okay”

  • “Bye”

  • “Bye”

Conclusion

  • TCP exists because the internet cant be trusted

  • The 3-way handshake ensures safe communication before data flows

  • Sequence numbers and ACKs keep data correct and ordered

  • TCP closes connection politely

Once we understand TCP, concepts like HTTP, HTTPS and Websockets make a lot more sense.