Skip to main content

Command Palette

Search for a command to run...

TCP Working: 3-Way Handshake & Reliable Communication

Understanding the TCP 3-Way Handshake Process

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

Hi, I’m Abdul Samad. A web development learner and tech enthusiast. I write about what I learn, share practical coding tips, and publish in-depth blogs on programming and modern web development.

Check out my full collection of blogs on Hashnode: https://abdulsamad30.hashnode.dev/

Connect with me on X for quick updates and insights: @abdul_sama60108

1. What is TCP and Why Do We Need It?

TCP stands for Transmission Control Protocol. It is one of the main protocols of the Internet, sitting on top of IP (Internet Protocol). TCP’s job is to make sure data moves between computers reliably.

Without TCP, data would just be sent randomly over the network. Imagine sending a letter without an envelope or address. Some letters might get lost, some might arrive out of order, and some might arrive multiple times.

2. What Happens if Data is Sent Without Rules?

If computers just send data without any rules:

  1. Data loss: Some packets might never reach the destination.

  2. Out-of-order arrival: Packets could arrive in the wrong order.

  3. Duplicate data: Same data might arrive more than once.

  4. No way to check errors: Corrupted data could be accepted as correct.

This is why TCP is needed. It acts like a delivery service that:

  • Checks that every packet arrives

  • Makes sure packets are in order

  • Detects errors and requests a resend

  • Makes communication reliable


3. Problems TCP Solves

TCP solves multiple problems in networking:

ProblemHow TCP Solves It
Lost packetsRetransmits missing packets
Out-of-order packetsReorders packets correctly
Duplicate packetsDetects and removes duplicates
CorruptionUses checksums to detect errors
No coordinationEstablishes a connection before sending data

So TCP ensures that if you send data, it will reach correctly and in order.

4. The TCP 3-Way Handshake

Before TCP can send data, it first establishes a connection between the sender and receiver. This is called the 3-way handshake.

Think of it like two people making sure they are ready before talking on the phone.

The three steps are:

  1. SYN – The sender says hello and wants to start communication.

  2. SYN-ACK – The receiver replies saying hello back and is ready.

  3. ACK – The sender confirms, and now the connection is established.


Step-by-Step Working

Let’s assume Computer A wants to send data to Computer B.

  1. Step 1: SYN (Synchronize)

    • Computer A sends a packet with the SYN flag set.

    • This packet asks Computer B: “Can we start talking? I want to send data.”

    • It also includes a sequence number, which will be used to keep track of packets.

  2. Step 2: SYN-ACK (Synchronize-Acknowledge)

    • Computer B receives the SYN.

    • It replies with a packet that has both SYN and ACK flags set.

    • SYN means “I am ready too.”

    • ACK means “I received your request and your sequence number.”

  3. Step 3: ACK (Acknowledge)

    • Computer A receives the SYN-ACK.

    • It sends back an ACK packet to Computer B.

    • This confirms that the connection is established.

Once this handshake is complete, data can flow reliably between the two computers.


Why the 3-Way Handshake is Important

  1. It ensures both sides are ready.

  2. It sets initial sequence numbers, so TCP can track packets.

  3. It prevents data loss or confusion at the start of the connection.

Without this handshake, you could start sending data blindly, which might get lost or corrupted.

5. How Data Transfer Works in TCP

Once the 3-way handshake is complete, TCP starts sending data.

Here’s the process at a high level:

  1. Breaking data into packets

    • TCP divides your large data (like a file or message) into smaller pieces called segments.

    • Each segment has a sequence number so the receiver can reorder them correctly.

  2. Sending packets

    • TCP sends these segments one by one over the network.
  3. Receiving and acknowledging

    • The receiver checks each segment for errors using a checksum.

    • For every segment received correctly, the receiver sends an ACK (acknowledgement) with the next expected sequence number.

6. How TCP Handles Packet Loss and Retransmission

  • If a packet does not arrive or an ACK is not received within a timeout, TCP retransmits that packet.

  • If multiple packets are lost, TCP can use fast retransmit after detecting missing sequence numbers.

  • This ensures that all data eventually arrives correctly.

7. How a TCP Connection is Closed

TCP connections are closed gracefully to ensure all data is delivered. This is done using a FIN and ACK process.

  1. FIN (Finish)

    • The side that wants to close the connection sends a FIN packet: “I am done sending data.”
  2. ACK (Acknowledge)

    • The other side acknowledges the FIN with an ACK: “I received your finish request.”
  3. Closing from the other side

    • The second side sends its own FIN when it is done sending data.

    • The first side replies with an ACK.

After this 4-step process, the connection is fully closed.

8. TCP Data Transfer with Sequence and ACK Numbers

TCP uses sequence numbers and ACK numbers to make sure data reaches safely and in the correct order.


1. Sequence Numbers (What They Are)

  • Every byte of data gets a number.

  • Helps the receiver know where each byte belongs.

Example:

Data: Hello World

ChunkBytesSequence Number
Hel1-31
lo4-54
Wo6-76
rld8-108

Think of sequence numbers like page numbers in a book.


2. ACK Numbers (What They Are)

  • After receiving data, the receiver sends ACK (acknowledgement) back to the sender.

  • It tells the sender:

    “I received all bytes up to this number, send the next ones.”

Example:

  • Computer B receives Hel (bytes 1–3) → sends ACK = 4

  • Meaning: “I got bytes 1, 2, 3. Now send byte 4 next.”


3. How Sequence + ACK Work Together

Step-by-step:

  1. Sender splits data into chunks with sequence numbers.

  2. Receiver checks each chunk for errors.

  3. Receiver sends ACK for the last correct byte received.

  4. Sender sends the next chunk based on ACK.

  5. Lost chunks are automatically resent because the receiver does not ACK missing bytes.


5. Why This Is Important

FeatureHow It Helps
ReliabilityLost chunks are detected and resent.
Correct OrderSequence numbers let receiver reorder chunks.
Error CheckingCorrupt chunks are ignored until resent.

10. TCP connection lifecycle (establish → transfer → close)