Skip to main content

Command Palette

Search for a command to run...

TCP Working: 3-Way Handshake & Reliable Communication

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

What is TCP and why it is needed

Before we get to the TCP, let's see where the TCP came from.
Basically What was the problem people faces and then came with the solution of TCP.

Once upon a time, in a beautiful village, there lived an old fisherman with his son. Every morning he would go to catch fish and in the evening he would pack the fish in a container and send it to the fish market to sell. But one thing that bothered his son was that his fish did not sell much in the market. He noticed that while taking the fish to the market, all the fish would die on the way. He told this to his father and his father came up with an idea. He said put water in the container and then the fish. Now the fish survived but then a problem arose that the weight of the container increased due to the water. Then the father found a sustainable solution, We’ll transport it by bicycle. Now Fish stayed alive, Delivery became reliable, They could deliver even in bulk, Business became sustainable.

Here’s the tech analogy:

IP address is like fish market location.
It helps you reach the destination, but doesn’t guarantee the fish will survive.

TCP is like water + bicycle transport.
It ensures safe and reliable delivery.

  • If fish doesn’t reach → resend it (Retransmission)

  • Market confirms delivery (ACK)

  • Fish arrive in correct order (Ordering)

  • Don’t overload delivery system (Flow / Congestion control)

Now Let’s see the defination:

Transmission Control Protocol (TCP) is a communications standard that enables application programs and computing devices to exchange messages over a network. It is designed to send packets across the internet and ensure the successful delivery of data and messages over networks.

in simple words, TCP/IP is a set of rules that tells computers:

  • How to send data

  • How to break into pieces

  • How to route it across Networks

  • How to make sure it reaches correctly

Now the question arises why we need it ?

let’s understand it.

Suppose there were two people on the internet:
Client A
Client B

And Client A wants to send a simple message: “Hi xyz”

Sounds easy, right?

But the internet is not a single straight road.
It’s like a huge network of roads, junctions, and traffic.

So this message has to travel through many different networks and routers before reaching Client B.

Now imagine these problems:

  • What if the message gets lost in the middle?

  • What if the message reaches Client B in the wrong order?

  • What if part of the message gets missing?

  • What if multiple messages get mixed up?

  • What if the message has to pass through 100+ networks before reaching?

So the big question becomes: “How can we ensure the message reaches the other person correctly, completely, and in the right order?”

That’s exactly why we need TCP/IP.

TCP/IP acts like a smart delivery system that makes sure communication on the internet is reliable, even when the path is unpredictable.

Problems TCP is designed to solve

Imagine the world before a common language existed…

  • India speaks Hindi

  • America speaks English

  • Japan speaks Japanese

  • France speaks French

  • and so on ..

Now suppose a person from India wants to talk to a person in America.

They have 2 problems:

  1. How to reach the person? (where is he?)

  2. How to communicate clearly? (what is he saying?)

So the world agrees on a common “communication system”.

Same thing happened with the internet:

In the beginning, many networks existed (like different countries), and each network had its own rules.

So networks couldn’t talk to each other easily.

That’s why a universal set of rules was created:

TCP/IP = The common language of the internet

So any computer, from any network, in any country can communicate.

basically, TCP/IP is like a universal language that allows different networks to talk to each other and exchange data reliably.

What is the TCP 3-Way Handshake

After these 3 steps, both are confident:

Now we can say that, Connection is ready and we can now send actual data.

In the same way TCP/IP 3-way handshake work.

lets see that:
TCP uses a process called 3-way Handshake for reliable communication. In 3-way Handshake process, three TCP segments are exchanged between the sender and the receiver to establish a reliable connection. These are called SYN, SYN-ACK and ACK.

Step1 (SYN): In the first step, client wants to establish a connection with server, so it sends a segment with SYN (Synchronize Sequence Number). This informs server that client is likely to start communication and with what sequence number it starts segments with.

Step2 (SYN + ACK): In the second step, Server responds to the client request with SYN-ACK signal bits set. Acknowledgement(ACK) signifies the response of segment it received and SYN signifies with what sequence number it is likely to start the segments with.

Step3 (ACK): In the last step, client acknowledges the response of server by ACK message. Status on both sides change to ESTABLISHED and both get ready to start the actual data transfer.

The steps 1 and 2 establish the connection parameter (seq nos.) for one direction and it is acknowledged. The steps 2 and 3 establish the connection parameter (seq nos.) for the other direction and it is acknowledged. After the whole process, a full-duplex communication is established. ISN (Initial Sequence Numbers) are randomly selected while establishing connections between client and server. Below is the simplified view of TCP 3-way handshake process.

Any device using this 3-way Handshake process resends the data unit until it receives an acknowledgement. Transport Layer checks the data with checksum functionality for Error Detection at the receiver side. If the data unit received at the receiver’s end is damaged, then receiver discards the segment. So, the sender has to resend the data unit for which positive acknowledgement is not received.

TCP allows one side/device to establish a connection. The other side/device may either accept the connection or refuse it. The device that is establishing/requesting the connection is called client and the side waiting for a connection is called server. TCP uses virtual ports to create a virtual end-to-end connection that can reuse the physical connections between two computers. TCP is encapsulated within the data field of IP datagrams and TCP encapsulates higher level protocol data such as Network Mgmt (SNM), HTTP (web), SMTP (email) and many other protocols.

How data transfer works in TCP

TCP is basically the responsible delivery system of the internet.

Let’s try to understand through real-life example:

Suppose you want to send a big book to your freind let say book of page 1000. But you don’t want to send it as a one huge box.

Instead, you split into pieces of packets:

  • Box 1

  • Box 2

  • Box 3

  • Box N

TCP does the same.

Step 1: TCP breaks data into small pieces (Segments)

example you send a large file to your freind , TCP divides that into small packets called segment.

Step 2: TCP adds “numbers” to every piece

each segment get sequence number like:

  • segment 1 (seq 1)

  • segment 2 (seq 2)

  • segment 3 (seq 3)

But Question Arises Why,

So receiver can:

  • arrange in correct order

  • detect missing pieces

Step 3: TCP sends segments and waits for confirmations (ACK)

After receiver gets segments, it sends back:

ACK (Acknowledgment), means yes i recieve this part.

Step 4: TCP ensures reliability (retransmission)

Think like, What if segment get lost ?

Receiver will not send ACK for that missing part.

Sender waits… and then:

Re-sends the missing segment.

Step 5: TCP ensures correct order (even if packets arrive mixed)

Sometimes we don’t get segment in sequence/order due to some router issues

Receiver gets:

  • segment 1

  • segment 3

  • segment 2

TCP will:

  • store them temporarily

  • rearrange

  • only then give to application

Step 6: Receiver re-creates the original data

Receiver takes: segments → arranges → joins → original message/file created

then apps get the final result.

How TCP ensures reliability, order, and correctness

TCP make sure reliability, order, and correctness by using a few smart techniques while sending data over the internet. First, TCP breaks the data into small parts (called segment) and gives each part a sequence number, so the receiver can arrange everything in the correct order even if packets arrive randomly. Next, the receiver sends an ACK (acknowledgement) back to confirm which parts have been received successfully. If the sender does not receive an ACK within a certain time, it assumes that packet was lost and retransmits (sends it again), which makes the delivery reliable. Finally, TCP also checks for errors using a checksum—if any part of the data is corrupted during travel, the receiver rejects it and TCP sends that part again. Because of sequence numbers, acknowledgements, retransmission, and checksum verification, TCP makes sure the data reaches the destination completely, in the correct order, and without mistakes.

How a TCP connection is closed

How mechanism works In TCP :

  • Step 1 (FIN from Client): The client sends a FIN to the server to start closing and enters the FIN_WAIT_1 state.

  • Step 2 (ACK from Server): The server acknowledges the FIN and the client moves to FIN_WAIT_2.

  • Step 3 (Client Waiting): The client waits for the server’s FIN while in the FIN_WAIT_2 state.

  • Step 4 (FIN from Server): The server sends its FIN after completing its closing tasks.

  • Step 5 (ACK from Client): The client acknowledges the server’s FIN, enters TIME_WAIT, and after a set delay, the connection closes fully.

More from this blog

U

Understanding Network Devices

10 posts