TCP Working: 3-Way Handshake & Reliable Communication
Understanding the TCP 3-Way Handshake Process

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:
Data loss: Some packets might never reach the destination.
Out-of-order arrival: Packets could arrive in the wrong order.
Duplicate data: Same data might arrive more than once.
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:
| Problem | How TCP Solves It |
| Lost packets | Retransmits missing packets |
| Out-of-order packets | Reorders packets correctly |
| Duplicate packets | Detects and removes duplicates |
| Corruption | Uses checksums to detect errors |
| No coordination | Establishes 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:
SYN – The sender says hello and wants to start communication.
SYN-ACK – The receiver replies saying hello back and is ready.
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.
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.
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.”
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
It ensures both sides are ready.
It sets initial sequence numbers, so TCP can track packets.
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:
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.
Sending packets
- TCP sends these segments one by one over the network.
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.
FIN (Finish)
- The side that wants to close the connection sends a FIN packet: “I am done sending data.”
ACK (Acknowledge)
- The other side acknowledges the FIN with an ACK: “I received your finish request.”
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
| Chunk | Bytes | Sequence Number |
| Hel | 1-3 | 1 |
| lo | 4-5 | 4 |
| Wo | 6-7 | 6 |
| rld | 8-10 | 8 |
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 = 4Meaning: “I got bytes 1, 2, 3. Now send byte 4 next.”
3. How Sequence + ACK Work Together
Step-by-step:
Sender splits data into chunks with sequence numbers.
Receiver checks each chunk for errors.
Receiver sends ACK for the last correct byte received.
Sender sends the next chunk based on ACK.
Lost chunks are automatically resent because the receiver does not ACK missing bytes.

5. Why This Is Important
| Feature | How It Helps |
| Reliability | Lost chunks are detected and resent. |
| Correct Order | Sequence numbers let receiver reorder chunks. |
| Error Checking | Corrupt chunks are ignored until resent. |
10. TCP connection lifecycle (establish → transfer → close)





