DEV Community

Cover image for TCP Working: 3-Way Handshake & Reliable Communication
Debashis Das
Debashis Das

Posted on

TCP Working: 3-Way Handshake & Reliable Communication

When data is sent over the internet , it doesn't magically reach the other side safely.
Packets can get lost, arrive could be out of order, or never arrive at all.

That's why TCP (Transmission Control Protocol) exists.

TCP makes sure that two computers:

  • agree before they start talking
  • send data in the correct order
  • and confirm that everythign is received properly

Before any real data is sent, TCP first creates a connection using a process called the 3 way handshake.
After that it carefully manages data transfer to ensure reliablity, correctness, and completeness.

In this article, we'll understand how TCP works - steps by steps - using simple language, real-life example, and clear diagrams, without diving into complex internals.


1. What is TCP and Why it is needed

TCP (Transmission Control Protocol) is a set of rules that helps computers communicate reliability.

It's job is simple:

Make sure data reaches the other side correctly, completely, and in order.

Without TCP the internet would feel unreliable and messy.


2. Problem TCP Is Designed to solve

If data is send without rules, many problems can heppen:

  • Data packets can get lost
  • Data can arrive out of order
  • Data can be duplicate
  • Sender may not know if the receiver got the data

TCP solves all of these problems by checking, comfirming and retrying.

3. What Is the TCP 3-Way Handshake

Before sending real data , TCP first creates a connection.
This setup process is called 3-way handshake

Think of it like starting a phone call. In phone call you don't start talking immediately - first, both sides
confirm they are ready.


4. Step-by-Step: SYN, SYN-ACK, ACK

Let's walk through it slowly.

step 1: SYN (client-server)

Client says:

"Hey server, I want to talk , Are you there?"

This message is called SYN (Synchronize).

Step 2: SYN-ACK (Server → Client)

Server replies:

"Yes, I am here and ready."

This reply is called SYN-ACK.

Step 3: ACK (Client- Server)

Client confirms:

"Great, let's start."

This final message is Ack.
Now the connection is established

Diagram 1: TCP 3-Way Handshake

flowchart LR
    A[Client] -->|SYN| B[Server]
    B -->|SYN-ACK| A
    A -->|ACK| B
Enter fullscreen mode Exit fullscreen mode

5. How Data Transfer Works in TCP

Once the connection is open, data starts flowing.

TCP does not send data as one big piece.
It breaks data into small packets.
Each packet has:

  • a sequence number
  • so the receiver know the correct order

After receiving packets, the receiver sends back ACKs (acknowledgements).


6. How TCP Ensures Reliability, Order, and Correctness

TCP is smart, and it constantly checks:
Reliabilty

  • If packet is missing -> re-send
  • If ACK is not received -> retry

Correct order

  • Uses sequesnce number
  • Rearranges packets if needed

Correct data

  • Detects corrupted packets
  • Requests retranmission

Diagram 2: Data Transfer With ACKs

flowchart LR
    A[Client sends data seq 1] --> B[Server]
    B -->|ACK 1| A
    A -->|seq 2| B
    B -->|ACK 2| A
Enter fullscreen mode Exit fullscreen mode

7. Packet Loss and Retransmission

Sometimes packets get lost due to:

  • network congetion
  • weak signals
  • router issues

TCP handles this automitically.
If ACK is not receives:

i did not got the confirmation - i'll send it again."


Diagram 3: Packet Loss Handling

flowchart LR
    A[Client sends packet] --> B[Packet lost]
    A -->|Timeout| C[Resend packet]
    C --> D[Server receives]
    D -->|ACK| A
Enter fullscreen mode Exit fullscreen mode

8. How a TCP Connection Is Closed

When communication is done, TCP close the connection properly.

It uses:

  • FIN-> "I am done sending"
  • ACK-> "I got it"

Both side confirm before closing.


Diagram 4: TCP Connection Lifecycle

flowchart LR
    A[Connection setup] --> B[Data transfer]
    B --> C[FIN sent]
    C --> D[ACK received]
    D --> E[Connection closed]
Enter fullscreen mode Exit fullscreen mode

Final Mental Model (Easy to Remember)

  1. TCP works like a careful connection:
  2. Say hello (3-way handshake)
  3. Talk clearly (data + ACKs)
  4. Repeat if needed (retransmission)
  5. Say goodbye properly (Fin + ACK)

One-line Takeaway

TCP makes sure data is delivered safely, in order and completely

Top comments (0)