Skip to main content

Command Palette

Search for a command to run...

TCP Working:

3-Way Handshake & Reliable communication

Published
5 min readView as Markdown

What is TCP and why it is needed

TCP (Transmission Control Protocol) is a core internet protocol that makes sure data is delivered correctly, completely, and in the right order between two devices.

What is TCP?

TCP is a connection-oriented protocol.
Before sending data, it establishes a connection between the sender and receiver, then manages the data transfer carefully.

Why is TCP needed?

TCP is needed because it guarantees reliable, ordered, and error-free communication over an unreliable internet.

Why is TCP needed?

The internet itself is unreliable—data travels in small chunks (packets) and:

  • packets can get lost

  • packets can arrive out of order

  • packets can get duplicated

    Problems TCP is designed to solve

    1. Packet loss

      • Data packets may disappear during transmission

      • TCP detects loss and retransmits missing packets

    2. Out-of-order delivery

      • Packets can arrive in any order

      • TCP reorders them correctly before giving data to the application

    3. Data corruption

      • Bits can get damaged while traveling

      • TCP checks errors and requests retransmission

    4. Duplicate packets

      • The same packet may arrive more than once

      • TCP removes duplicates

    5. Unreliable delivery

      • No guarantee data will reach the destination

      • TCP guarantees reliable delivery

    6. Sender faster than receiver

      • Receiver may not handle data so fast

      • TCP uses flow control to match speeds

    7. Network congestion

      • Too much traffic can collapse the network

      • TCP reduces sending rate during congestion

    8. No session management

      • Network doesn’t know when communication starts or ends

      • TCP establishes and closes connections properly

What is the TCP 3-Way Handshake

The TCP 3-Way Handshake is the connection-establishment mechanism used by TCP to create a reliable, full-duplex communication channel between two devices (usually a client and a server) before any application data is exchanged.

The TCP 3-Way Handshake is a three-step process used to establish a reliable TCP connection between a client and a server. In the first step, the client sends a SYN packet to request a connection and synchronize sequence numbers. In the second step, the server responds with a SYN-ACK packet, acknowledging the client’s request and providing its own sequence number. In the third step, the client sends an ACK packet acknowledging the server’s sequence number. After this exchange, both sides enter the established state and reliable data transfer can begin.

Step-by-step working of SYN, SYN-ACK, and ACK:-

In TCP 3-Way Handshake, the client sends SYN to request a connection, the server replies with SYN-ACK to acknowledge and synchronize, and the client sends ACK to confirm, after which the connection is established.

Step 1: SYN — Connection Request

Client → Server

  • Client sends a TCP segment with:

    • SYN flag = 1

    • Sequence Number = X (random number)

Step 2: SYN-ACK — Connection Acknowledgment

Server → Client

  • Server replies with:

    • SYN flag = 1

    • ACK flag = 1

    • Sequence Number = Y (server’s random number)

    • Acknowledgment Number = X + 1

Step 3: ACK — Final Confirmation

Client → Server

  • Client sends:

    • ACK flag = 1

    • Acknowledgment Number = Y + 1

How data transfer works in TCP

In TCP, data transfer occurs after a connection is established using the three-way handshake. The sender divides application data into smaller segments and assigns sequence numbers to each byte. The receiver checks each segment for errors and sends acknowledgements for successfully received data. If any segment is lost or corrupted, TCP retransmits it. Flow control and congestion control mechanisms regulate the data rate, ensuring reliable, ordered, and efficient data transfer.

1. Data is broken into segments

2. Sequence numbers are assigned

3. Segments are sent to IP layer

4. Receiver checks data

5. Acknowledgements (ACKs)

6. Retransmission of lost data

7. Flow control (Receiver-based)

8. Congestion control (Network-based)

9. In-order delivery to application

10. Continuous full-duplex communication

How TCP ensures reliability, order, and correctness

TCP ensures reliability by using acknowledgements, timers, and retransmissions to guarantee delivery of data. It maintains correct order using sequence numbers and buffering of out-of-order segments. Correctness is ensured through checksums for error detection and removal of duplicate packets. Together with flow control and congestion control, TCP provides reliable, ordered, and error-free data transfer.

1️⃣ How TCP Ensures Reliability

Reliability = data must reach the destination

a) Acknowledgements (ACK)

b) Retransmission

c) Timers

2️⃣ How TCP Ensures Order

Order = data arrives to application in the same order it was sent

a) Sequence Numbers

b) Buffering

3️⃣ How TCP Ensures Correctness

Correctness = data is not corrupted or duplicated

a) Checksum (Error Detection)

b) Duplicate Detection

4️⃣ Supporting mechanisms that strengthen reliability

Flow Control

  • Prevents sender from overwhelming receiver

  • Uses sliding window

Congestion Control

  • Prevents network overload

  • Adjusts sending rate dynamically

    How a TCP connection is closed

    TCP closes a connection gracefully so that no data is lost.
    Unlike connection setup (3-way handshake), connection termination uses a 4-step process, often called the 4-Way Handshake.

    Why TCP needs a proper closing process

    • TCP is full-duplex (both sides send data independently)

    • Each direction must be closed separately

    • Ensures all sent data is fully received

A TCP connection is closed using a four-way handshake. The side initiating the closure sends a FIN segment indicating it has no more data to send. The receiver acknowledges this FIN with an ACK. When the receiver has finished sending its remaining data, it sends its own FIN, which is acknowledged by the initiator with a final ACK. After waiting in the TIME-WAIT state to handle delayed packets, the connection is fully closed.