Skip to main content

Command Palette

Search for a command to run...

TCP Working: 3-Way Handshake & Reliable Communication

Published
5 min readView as Markdown

When two computers are connecting with each other within a network, they need some standard set of rules that they need to follow is called Internet Protocol (IP). If data is sent without any rules then it become very difficult for device to process the information . That’s why it is very important to use protocol which every device should use to process information. One of the main protocol is TCP which is used along with IP in order to deliver data in reliable way. IP protocol is used to deliver the data to a particular address.

What is TCP ?

TCP stands for Transport Control Protocol. TCP is a transport layer (Layer 4 of OSI model) protocol that ensures reliable data delivery. It is meant to be used with IP, so these protocols are referenced as TCP/IP protocol. TCP protocol is used when accuracy for delivered data is more important than speed.

Some main features of TCP are

  1. Connection-oriented Protocol

  2. Reliable and ordered delivery of data

  3. Higher overhead but higher accuracy

Problems TCP is designed to solve

The main problems are

  1. Sends all data with accuracy, don’t send corrupt data.

  2. Data should be transmitted in ordered way.

  3. In case of data loss, data should be retransmitted.

  4. Tracking of delivered data , how much data is transmitted.

  5. Duplication of data packets , TCP detects and discards duplicate data packets.

What is the TCP 3-Way Handshake?

Before data flows between devices over the Internet, we have to establish connection between these two devices , that is called handshake. You can consider like before two people starts communicating with each other , they do handshake , then start communicating each other.

Why three way handshake ? Because there are three steps involved in establishing a connection.

Step 1 - SYN( Synchronize) Client initiate the connection by sending a request with flag SYN as a random initial sequence number (ISN). Client enters in SYN-SENT state.

Step 2 - SYN-ACK (Synchronize-Acknowledge) Now, server receive the SYN and responds with its own SYN flag and acknowledges the client’s sequence number with ACK = (client’s ISN+1) flag. Server enter in SYN-RECEIVED state.

Step 3 - ACK (Acknowledge) In this step, client acknowledges server’s sequence numbers by sending ACK = (server’s ISN + 1) flag. Both client and server enters in ESTABLISHED state.

Connection is established and ready for data transfer.

How data transfer works in TCP?

After establishing connection, now flow of data starts. Each byte of data is numbered sequentially to keep tracking the order of data.

Step 1 - Server sends data with SYN flag as initial sequence numbers plus size of bytes of data that it is sending to client.

Step 2 - When client receives data then client sends back an acknowledgement (ACK) flag and what it expects next.

TCP is full-duplex, meaning both sides can send and receive simultaneously. Each direction maintains its own sequence numbers.

How TCP ensures reliability, order, and correctness?

As sequential numbers are attached with data packets, TCP uses this to reassemble all data packets and ensure that data packets are in order.

Every data packets needs acknowledgements. If server does not receive ACK within a specific timeout period, then something wrong occurs and it retransmit the data packets.

Two ways TCP detects loss:

a) Timeout-Based Retransmission

Sender starts a timer while sending data to client. If ACK doesn't arrive before timeout, it is assumed packet was lost and retransmit the data. TCP measures Round-Trip Time (RTT) and timeout is set based on RTT (typically RTT + variance buffer) . Variance buffer depends on the network speed.

b) Fast Retransmit (Duplicate ACKs)

If server sends 4 packets , receiver gets three packets , one packet is missing. It sends ACK for packet 3 one after another. When sender receives three duplicate ACKs, it assumes packet 4 is lost. Sender immediately retransmits packet 4 without waiting for timeout.

TCP detects correctness using checksums. Checksum includes TCP Header, data and parts of the IP header. Client calculates checksum and send it along with data packets. Server recalculates checksum and if checksums don't match, data was corrupted. So, server discards the segment (doesn't ACK it). Client retransmits due to missing ACK.

How a TCP connection is closed?

TCP also terminate the connection. This is done through a four-way handshake (also called connection termination).

Step-1 FIN from Client — Client sends FIN flag to server for closing connection with server.

Step-2 ACK From Server - Server receives the FIN and sends back ACK acknowledging the SYN flag.

Step-3 FIN from Server - Server stops sending any remaining data and sends its own FIN flag.

Step-4 ACK from Client - Client receives server's FIN and sends final ACK.

After send final ACK, client does not immediately close the connection , it waits for two times the Maximum Segment Lifetime (MSL) to ensure that final ACK is received by server and prevent old duplicates.

Conclusion

Understanding TCP working gives you insight into how the internet actually works behind the scenes. When you see connection refused or Invalid Response Received, you now know what's happening at the TCP level.

As you continue learning software development concepts, this foundational knowledge will help you whether you're debugging network issues, optimizing application performance, or designing distributed systems.