DEV Community

Vraj Bhavsar
Vraj Bhavsar

Posted on

📈From Zero to DevOps: Week 4 – Building a Strong Foundation in Linux Networking- TCP Working

TCP(Transmission Control Protocol)

TCP is a connection oriented , reliable protocol at the the transport layer of the OSI model. it ensures that data sent from one device reaches correctly and in order on the other side.

1. TCP Connection: The 3-way Handshake🤝

When a device wants to send data — like your browser opening google.com — the first step is to establish a reliable connection between your device and the server. This is done through the TCP 3-way handshake, a fundamental part of how data travels across the internet reliably.

Step 1: SYN (Synchronize)
It all begins with your device (the client) saying:
“Hey server, I’d like to start a connection. Here’s my Initial Sequence Number (ISN): 1000.”

This message is called a SYN packet, and it’s the client’s way of knocking on the server’s door and saying, "Let’s sync up!"

📌 What is a Sequence Number?

  • A sequence number is a number that uniquely identifies each byte of data sent over a TCP connection. It helps the receiver keep track of the order of the bytes and detect missing or out-of-order packets.

Step 2: SYN-ACK(Synchronize-Acknowledge)

The server receives the SYN packet and says:
“Got your request! I’m ready to talk. Here's my own Initial Sequence Number: 5000. Also, I acknowledge your sequence number 1000.”

This is the SYN-ACK packet — the server both synchronizes with its own sequence number and acknowledges the client's request.

At this point, both devices are aware that the other is alive and ready to communicate.

Step 3: ACK (Acknowledge)
The client receives the SYN-ACK and responds with one final message:
“Thanks! I got your sequence number 5000, and I’m ready to roll!”

This is the ACK packet. It confirms receipt of the server’s sequence number and finalizes the handshake.

✅ Connection Established!
Now, with the handshake complete, both client and server are ready to exchange data in a reliable, ordered, and error-checked way — just what TCP promises.

This three-step handshake ensures that both ends of the connection are ready, no data is lost at the start, and that the communication can begin smoothly and reliably.

2. Data Transmission🚚

Now, once the connection is established between the client and the server, data transmission begins.

TCP first splits the data into segments (packets). Each segment carries a unique sequence number that helps the receiver arrange the data in the correct order.

As soon as the receiver gets a segment, it sends back an ACK (acknowledgment) confirming the successful receipt of that data.

But what if a segment is lost during transmission?

TCP is smart — if, say, Segment 2 is lost, TCP detects the missing segment and slows down the transmission, then retransmits only that segment. It ensures no data is lost, duplicated, or delivered out of order.

Example:

Segment 1: Sequence Number = 1000, Size = 500 bytes

Receiver replies with ACK 1500 → meaning: “I received everything up to byte 1499. Please send the next data from byte 1500.”

This mechanism is what makes TCP a reliable protocol, perfect for tasks like web browsing, file transfers, and emails — where accurate data delivery is crucial.

3. TCP Flow Control💡

Imagine the sender is transmitting data very fast, but the receiver is processing it slower — like pouring water into a bottle with a narrow neck. If the sender doesn't slow down, the receiver might get overwhelmed.

To handle this, TCP uses a “window size” mechanism.

The receiver tells the sender:
🗣️ “Hey, I can accept 4096 bytes right now.”

This value is called the receive window size, and it represents how much data the receiver's buffer can handle at that moment.

Now, the sender must pause or slow down if it fills that window.

As the receiver processes the incoming data, it updates the window size and informs the sender that it's ready to receive more. This way, both stay in sync.

✅ This technique prevents buffer overflow and ensures smooth, efficient communication, even if one side is faster than the other.

4. TCP Congestion Control 🌐

Now imagine not just one sender and receiver, but hundreds of devices communicating over the same network. If everyone sends data at full speed, the network gets congested — like a traffic jam!

To prevent this, TCP uses congestion control algorithms. One of the classic ones is called slow start.

When a connection begins, TCP starts slow. It sends a small amount of data and waits for acknowledgments. If everything goes well, it gradually increases the sending rate — like slowly pressing the gas pedal in traffic.

But the moment it notices packet loss (a sign of congestion), it immediately slows down, cuts the speed, and tries again more carefully.

This adaptive behaviour helps TCP:

  • Avoid overwhelming the network

  • Use available bandwidth efficiently

  • Ensure fair sharing among devices

Think of it as TCP being polite on a crowded highway — it doesn't rush, and it makes room for others.

🔚 5. TCP Connection Termination (Graceful Close)

Just like a polite conversation ends with a goodbye from both sides, a TCP connection also closes gracefully — and it does so in four steps.

Once the client finishes sending its data, it sends a FIN (Finish) message to the server, saying:
“I’m done, and I’d like to close the connection.”

The server replies with an ACK, meaning:
“Okay, I’ve received your request to close.”
But the server might still be sending some data of its own.

When the server is also done, it sends a FIN back to the client:
“I’m done too. Let's wrap it up.”

Finally, the client sends one last ACK, confirming:
“Got it. Closing on my side too.”

✅ At this point, both ends have closed the connection properly. This clean closure ensures all data is sent and acknowledged before the connection is terminated.

Top comments (0)