TCP working : 3 - way Handshake & reliable communication
What is TCP and why is it needed?
TCP (Transmission Control Protocol) is a fundamental communication protocol that enables reliable data exchange between two devices over the Internet. It defines a set of rules that both the sender and receiver must follow to ensure that data is delivered correctly, completely, and in the proper order. TCP operates at the transport layer and acts as a dependable bridge between applications and the underlying network.
In everyday Internet usage, TCP works silently in the background. Whether you are opening a website, submitting a form, sending data from a frontend to a backend server, or uploading a file, TCP ensures that the data sent from your device reaches the server accurately and that the server’s response returns without loss or corruption.
The Internet itself is not reliable by design. Data is split into packets and sent through multiple routers and networks rather than a fixed path. Along the way, packets may be delayed, lost, duplicated, or arrive out of order due to congestion, routing changes, or hardware issues.
TCP is needed because applications require predictable and trustworthy communication despite this unreliable network behavior. It guarantees that data is delivered completely, arrives in the correct order, and remains uncorrupted, allowing applications to function correctly without worrying about network failures.
Without TCP, every application would need to build its own mechanisms for handling missing data, reordering packets, and retransmitting lost information. This would greatly increase complexity and result in inconsistent and unreliable behavior across applications.
Modern applications such as web browsing, online payments, authentication systems, and file downloads depend heavily on TCP. Without it, web pages could load partially, transactions might fail midway, login requests could be lost, and downloaded files could become corrupted—making the Internet frustrating and unreliable to use.
Problems TCP is designed to solve
One of the major problems TCP is designed to solve is packet loss. As data travels across the Internet, it is broken into small packets that pass through multiple routers and networks. Due to congestion, network failures, or buffer overflows, some packets may get dropped before reaching their destination. TCP detects missing packets using acknowledgements and sequence numbers, and it automatically retransmits the lost data to ensure complete delivery.
Packet Loss
When data travels across the Internet, it is split into packets and routed through multiple networks. Due to congestion, hardware failures, or buffer limits, some packets may never reach their destination. TCP solves this by tracking sent packets and retransmitting any data that is not acknowledged, ensuring nothing is lost.Out-of-Order Delivery
Packets often take different paths across the network, which means they may arrive in a different order than they were sent. Without correction, this would result in broken or unreadable data. TCP assigns sequence numbers to packets so the receiver can reorder them correctly before delivering the data to the application.Duplicate Packets
In certain scenarios, the same packet may be delivered more than once due to retries or routing issues. Processing duplicates would cause incorrect results. TCP detects duplicate packets using sequence numbers and discards them, making sure each piece of data is processed only once.Data Corruption
During transmission, packets can become corrupted because of noise or network issues. TCP includes checksum validation to detect corrupted data. If an error is found, the corrupted packet is discarded and requested again from the sender.Flow Control
Not all systems can process data at the same speed. A fast sender can easily overwhelm a slower receiver. TCP prevents this by controlling how much data can be sent at a time based on the receiver’s capacity, ensuring smooth communication.Network Congestion
When too many devices send data simultaneously, networks can become congested, leading to delays and packet loss. TCP continuously monitors network conditions and adjusts its sending rate to avoid overwhelming the network.Simplifying Application Development
By handling all these issues at the protocol level, TCP allows developers to focus on building applications instead of managing unreliable network behavior. Reliability, ordering, and correctness are guaranteed automatically.
What is the TCP 3-Way Handshake?
The TCP 3-Way Handshake is the process TCP uses to establish a reliable connection between a client and a server before any actual data is exchanged. It ensures that both sides are reachable, ready to communicate, and synchronized with each other.
Since the Internet is unreliable, TCP cannot assume that a device is available or listening. The handshake acts as a verification step, confirming that both the sender and receiver can send and receive data properly.
This process is called a three-way handshake because it involves three specific message exchanges between the client and the server. Only after all three steps are completed does TCP allow data transfer to begin.
The handshake also allows both sides to agree on initial sequence numbers, which are essential for tracking data order, detecting loss, and ensuring reliable delivery throughout the connection.
Why the 3-Way Handshake Is Important
It confirms that the server is alive and listening.
It confirms that the client can receive responses.
It prevents half-open or broken connections.
It prepares TCP mechanisms like sequence numbers and acknowledgements before data transfer starts.
Step-by-Step Working of SYN, SYN-ACK, and ACK
1️⃣ SYN (Synchronize)
The process begins when the client sends a SYN packet to the server. This packet indicates that the client wants to initiate a connection.
Along with the connection request, the client includes an initial sequence number, which will be used to track the order of data packets sent later.
At this stage, the client is essentially saying:
“I want to start a connection, and here is where my data numbering will begin.”The client now waits for a response from the server before sending any actual data.
2️⃣ SYN-ACK (Synchronize + Acknowledge)
When the server receives the SYN packet, it responds with a SYN-ACK packet.
This response serves two purposes:
It acknowledges the client’s request to establish a connection.
It sends the server’s own initial sequence number back to the client.
The server’s message can be interpreted as:
“I received your request, I’m ready to communicate, and here is my sequence number.”At this point, the server knows the client is reachable, but the connection is not fully established yet.
3️⃣ ACK (Acknowledge)
Finally, the client sends an ACK packet back to the server.
This packet confirms that the client has successfully received the server’s SYN-ACK and is ready to proceed.
With this acknowledgement:
Both sides now know each other’s sequence numbers.
Both sides confirm they can send and receive messages reliably.
Once the ACK is received, the TCP connection moves into the ESTABLISHED state.
What This Achieves
Both the client and server confirm two-way communication.
Initial sequence numbers are synchronized for reliable data tracking.
The connection is fully prepared for ordered and error-free data transfer.
How data transfer works in TCP?
Once the TCP connection is established, data transfer begins as a continuous stream, even though internally the data is split into smaller chunks. This stream-based approach allows applications to send and receive data smoothly without worrying about individual packets.
TCP breaks the data into segments and assigns each segment a sequence number. These sequence numbers help the receiver understand the correct order of the data, even if segments arrive out of sequence.
As data is received, the receiver sends back acknowledgements (ACKs) to confirm successful delivery. These ACKs tell the sender how much data has been received so far, enabling reliable tracking of progress.
If an acknowledgement is not received within a certain time, TCP assumes the data was lost and automatically retransmits the missing segments. This ensures that no part of the data stream is lost during transmission.
Throughout the transfer, TCP uses flow control and congestion control to adjust the sending rate based on the receiver’s capacity and network conditions, ensuring stable and efficient communication.
🛡 How TCP Ensures Reliability, Order, and Correctness
Reliability through Acknowledgements and Retransmission
TCP ensures reliability by requiring the receiver to acknowledge received data. If acknowledgements are missing or delayed, TCP automatically retransmits the lost segments, guaranteeing that all data eventually reaches its destination.Ordered Delivery using Sequence Numbers
Every TCP segment carries a sequence number that represents its position in the data stream. Even if segments arrive out of order, TCP rearranges them correctly before delivering the data to the application.Error Detection with Checksums
TCP uses checksums to verify the integrity of data during transmission. If a segment is found to be corrupted, it is discarded and retransmitted, ensuring that only correct data is delivered.Duplicate Detection and Elimination
Duplicate segments may occur due to retransmissions or network delays. TCP identifies these duplicates using sequence numbers and discards them, preventing repeated or inconsistent data delivery.Controlled Data Flow and Congestion Handling
TCP regulates how much data is sent based on the receiver’s capacity and current network conditions. This prevents buffer overflow, reduces packet loss, and maintains stable communication.
🔚 How a TCP Connection Is Closed
A TCP connection is closed gracefully, meaning both sides agree to end communication instead of cutting it off suddenly. This ensures that all remaining data is delivered before the connection is terminated.
When one side finishes sending data, it sends a FIN (Finish) message to indicate that it has no more data to transmit. This does not immediately close the connection but signals the intent to end communication.
The receiving side responds with an ACK (Acknowledgement) to confirm that the FIN was received. At this point, the sender cannot send more data, but it can still receive data from the other side.
When the second side is also done sending data, it sends its own FIN message. This ensures that both directions of data flow are properly closed.
Finally, the last ACK is sent, and the connection is fully terminated. TCP may briefly keep the connection in a waiting state to handle delayed packets before completely releasing resources.