Skip to main content

Command Palette

Search for a command to run...

TCP Working: 3-Way Handshake & Reliable Communication

Updated
5 min read

What is TCP, and why is it needed

If data is sent over the internet without any rules, it can get lost, arrive in the wrong order, or only reach halfway, and the receiver will not know what went wrong. To avoid this confusion, TCP is used. TCP is a set of rules that makes sure data is sent properly from one computer to another. It first establishes a connection, then sends data in steps, checks whether it is received, and resends it if something is missing. Because of TCP, essential services like websites, email, file downloads, and online payments work smoothly and safely.

Problems TCP is designed to solve

TCP is a set of rules that makes sure data sent over the internet reaches the other side safely, completely, and in the correct order, even though the network itself is unreliable. Without TCP, data could be lost, arrive out of order, or be duplicated. TCP turns this unreliable system into a dependable connection that applications can trust.

TCP fixes a few main problems.

First, it ensures delivery reliability by checking whether the data has arrived and resending it if something is missing or damaged.

Second, it keeps data in order, so the receiver receives information exactly as it was sent, without duplicates.

Third, it controls the flow of data so a slow device is not overwhelmed by too much information at once.

Fourth, it controls congestion by slowing down when the network is busy and speeding up when it is free, which helps keep the internet stable for everyone.

TCP also manages connections by properly starting and ending communication and by keeping different conversations separate using port numbers.

All this is important because applications such as websites, email, file transfers, and remote logins depend on data being correct and in order. TCP introduces some delay and is not ideal for live or real-time use, but it is essential for accuracy and reliability.

What is the TCP 3-Way Handshake?

The TCP 3-way handshake establishes a safe, reliable connection before any real data is sent. You can think of it as a normal conversation between two people, making sure both are ready to talk.

So, in simple words, the TCP 3-way handshake is just both sides saying hello, confirming they can hear each other, and agreeing to start talking properly. This process makes TCP reliable and ready for data transfer.

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

Imagine this:

Step 1 – SYN (Client says hello)

The client says, “Hello, can I talk to you?”

This message also includes: “I will start counting my data from this number.”

This is the SYN message.

Step 2 – SYN - ACK (Server replies)

The server replies: “Yes, I hear you. You can talk to me.”

It also says: “I got your message, and I will start counting my data from this number.”

This is the SYN-ACK message.

Step 3 – ACK (Client confirms)

The client responds: “Great, I got your reply. We are connected now.”

This is the final ACK message.

After these three steps, both sides know:

  • The other side is ready

  • Data sequence numbers are synchronized

  • Communication can start safely

How data transfer works in TCP

TCP is a transport-layer protocol that helps computers communicate reliably over a network. Before sending any data, TCP first ensures both sides are ready by establishing a connection through the three-way handshake. Once the connection is set, TCP breaks large data into small pieces called packets. Each packet is sent individually, and the receiver confirms receipt. If any packet is lost or damaged, TCP retransmits it. TCP also controls how fast data is sent, so the receiver is not overloaded and slows down if the network is busy. After all the data is sent successfully, TCP properly closes the connection using a closing process. Because of these steps, TCP ensures data is delivered safely, in order, and without errors, which is why it is used for websites, emails, file downloads, and other important applications.

How TCP ensures reliability, order, and correctness

TCP connection termination is just how computers politely end a connection after all data has been sent. It uses FIN and ACK messages so both sides clearly understand that the communication is finished.

First, one computer sends a FIN message, which means “I am done sending data.” The other computer replies with an ACK, indicating, “Okay, I got your message.” When the second computer is also done, it sends its own FIN message. Finally, the first computer sends an ACK to confirm it. After this back-and-forth, both computers know the work is complete, and the connection is safely closed.

How a TCP connection is closed

When a TCP connection is finished, it is closed in a polite and proper way called a four-way handshake. Either the client or the server can start this process when they feel the work is done. First, one side sends a FIN message, which simply means “I am done sending data.” The other side must reply with an ACK, saying “Okay, I received your message,” even if it is not ready to close yet. If the other side is also done, it then sends its own FIN message. Finally, the first side replies with an ACK, and the connection is safely closed.

Sometimes, if both sides already agree to close the connection, this can happen a little faster using a three-step process instead of four. But the idea is the same: both sides clearly say they are finished. If there is an emergency and the connection must be stopped immediately, one side can send an RST(reset) message, which means “stop right now.” In that case, the connection ends instantly without any further replies.

More from this blog

Prakash Jha Blogs

23 posts

Trying to write Blogs on day-to-day applications or technology from a different perspective, you never thought of how amazing those technologies are.