Skip to main content

Command Palette

Search for a command to run...

TCP Working: 3-Way Handshake & Reliable Communication

Updated
4 min read
TCP Working: 3-Way Handshake & Reliable Communication

TCP (Transmission Control Protocol) is a networking protocol that allows devices to communicate reliably over a network. It ensures reliable, ordered, and error-free transfer of data between applications. TCP works on the transport layer of the OSI model.

Why use TCP?

The question is “Why will we not communicate without TCP?” There are a lot of problems without TCP.

  • There is no agreement on how communication should start.

  • There is no guarantee that data will arrive.

  • There is no ordering of data received.

  • No mechanism to detect missing data.

  • No confirmation if data is received or not.

  • There is no agreement on closing the connection.

To solve these problems, TCP was built; it solves all these problems, which we will understand in detail in this article.

What is the TCP 3-Way Handshake?

Before sending the actual data, TCP establishes a connection and ensures that the receiver is available by a method called the 3-way handshake. Think of you need to start a conversation with a friend; you tell your friend, “Hey, are you there?” Your friend replies, “Yes, I’m here. Are you there?” You say, “Yes, I’m also here. Let’s talk.”

TCP does similarly before sending the data. These steps are SYN, SYN-ACK, and ACK. Let’s understand the step-by-step working of SYN, SYN-ACK, and ACK.

Step 1: SYN (Synchronise)

  • The sender sends a SYN packet to the receiver.

  • It means, “I want to connect."

Step 2: SYN-ACK (Synchronise-Acknowledge)

  • The receiver replies with SYN-ACK.

  • “I received your request.” (ACK).

  • “I’m ready too” (SYN).

Step 3: ACK

  • The sender sends an ACK.

  • It means “I received your response.”

With these steps the sender and receiver establish a successful connection, and both are ready to send and receive data.

How does data transfer work in TCP?

Segmenting

  • TCP breaks the data into smaller segments; each segment contains some data and headers.

  • Headers contain information like sequence number, port, and flag.

  • TCP does not care about routing and addressing; it’s handled by IP.

Routing via IP

  • After segments are created, these are handled to IP.

  • The IP address sends the segments through the network to the receiver, possibly through different paths.

Reassembly at Receiver

  • Segments may arrive out of order because they can take different paths through the network.

  • Segments are reassembled through their sequence numbers.

Acknowledgement (ACK)

  • The receiver sends ACK to the sender on each segment or group of segments it receives.

  • The sender waits for ACK; if it is not received for any segment, it retransmits that particular segment.

Retransmission

  • If the sender doesn’t receive ACK in a specific time, it assumes the segment is lost and retransmits it.

  • It ensures that every segment is received by the receiver, which makes TCP reliable.

How a TCP connection is closed

Closing a TCP connection is similar to opening a connection, but this time it’s a four-way handshake:

  1. FIN (Finish): The sender who wants to close the connection sends the FIN to the receiver.

  2. ACK (Acknowledge): The receiver acknowledges the FIN with ACK.

  3. FIN (Finish): The receiver then sends his own FIN when it’s ready to close the connection.

  4. ACK (Acknowledge): The sender responds with an ACK, completing the connection termination.

Conclusion

TCP is a networking protocol that allows the transmission of the data reliably, orderly, and error-free over a network. TCP solves a bunch of problems like communication starting agreement, data arrival confirmation, data ordering, retransmission of missing or lost data, acknowledgement on getting data, agreement on closing the connection to ensure data is fully transmitted.

TCP starts a connection with 3-way handshaking SYN → SYN-ACK → ACK. And similarly closes the connection by 4-way handshaking FIN → ACK → FIN → ACK.

I’m a fellow learner, just like you all. If there is anything missing or incorrect in this article, do let me know. I’m always open to learning and mistakes.

More from this blog

M

Mursaleen's Blog

13 posts