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

What is TCP and why it is needed?

TCP (Transmission Control Protocol) is a communication rule that helps devices send data reliably over the internet.

When you open the website and send the msg, or download any file , at that time the data is broken down into small packets and send across the network.

TCP makes sure that the pieces are reaches to destination without any packet loss with correct order.

But wait :

What if TCP is not present ?

  • Without TCP, data delivery would be messy and unreliable.

What happens if data is sent without Protocols?

If data is sent without TCP or rules then there is possibility of :

  • Some data packets get lost

  • order of packets may be change

  • Duplicate packets may appear

  • The receiver does not know that , is the data is correct or not

Imagine receiving a book where pages are missing or mixed up. That’s what the internet would look like without TCP.

Importance and uses of TCP

TCP acts like a careful courier service:

  • Confirms connection before sending data

  • Assign numbers to each packet

  • Waits for confirmation (ACK)

  • Resends data if something is lost i.e. retransmission

Uses of TCP:

  • Web browsing (HTTP/HTTPS)

  • Emails

  • File downloads

  • Online logins

What is the TCP 3-Way Handshake?

Before sending data, TCP establishes a connection using a process called the 3-Way Handshake.

It checks that , is both sides are ready for receiving and sending data.

TCP 3-Way Handshake

Step 1: SYN (Client to Server)

Client: “Hey, can we talk?”

This is the SYN (synchronize) message.

Step 2: SYN-ACK (Server to Client)

Server: “Yes, I’m ready. Are you ready too?”

This is SYN-ACK (synchronize + acknowledge).

Step 3: ACK (Client to Server)

Client: “Yes, I’m ready.”

This is ACK (acknowledgement).

Connection established. Data transfer can start.

How data transfer works in TCP

Once the connection is established:

  • Data is split into segments

  • Each segment gets a sequence number

  • The receiver sends an ACK for received data

Example:

  • Sender sends data with sequence number 1

  • Receiver replies: “I got 1, send next”

Sequence numbers & ACK

Sequence number : simply, it tells the order of data

ACK number : It confirms which data is received

This ensures:

  • Correct order

  • No missing data

  • No duplicate data

How TCP ensures reliability, order, and correctness

1.packet loss

  • If any packet is lost or lagging or if data is corrupted then it detect it

  • The solution is retransmission

2. Retransmission

  • when error occurs, TCP detects the error and request retransmission.

3.order

  • when your order of data is change then TCP rearranges them using sequence numbers

How a TCP connection is closed

When communication is complete, TCP closes the connection safely.

But it does not hanging up immediately. It closes the connection step by step so both sides know all data is received.

It is like , we are ending conversation politely on phone calls, not direct hanging up.

FIN means : I am done sending data (finish)

ACK means : I received your FIN

Step 1: FIN (Side A to Side B)

  • Side A has no more data to send

  • It sends a FIN packet to Side B

  • Side A can still receive data, just not send anymore

Step 2: ACK (Side B to Side A)

  • Side B confirms it got the FIN

  • Now Side B knows that Side A will not send more data

  • But Side B may still be sending its own data

At that stage connection is half closed and still data is flowing from side B to A

Step 3: FIN (Side B to Side A)

Once Side B finishes sending its data:

  • Side B send the FIN like “ I am also done sending data”

  • Side B has nothing more to send

  • Both sides are ready to close

Step 4: Final ACK (Side A to Side B)

Final ASK means “ I also received your FIN”

TCP safely closes the connection

Connection is now fully closed

Conclusion:

TCP closes a connection in a controlled and reliable way using FIN and ACK messages. Each side clearly tells when it has finished sending data and confirms receipt from the other side. This step-by-step process ensures all data is delivered properly and the connection ends without loss or confusion.

I hope that you’ve found this blog helpful...! If you have any questions or feedback, feel free to leave a comment below.

If you found this article helpful, please like and share it .