TCP Working: 3 Way Handshake & Reliable Communication
In this blog we would discuss about TCP working behind the scenes to ensure data is sent in a proper manner in a high level overview, but before we get straight to the working, we need to be aware of a few concepts and questions.
What is TCP and Why is it needed?
TCP stands for Transmission Control Protocol and is a transport layer protocol designed for the reliable communication and to share data efficiently between two devices over the internet.
Without TCP the internet would be a messed up place, imagine a scenario where you send some files to your friend, but your friend receives files in unordered manner making no sense, or he receives duplicate data or the entire thing has missing data or even corrupted data, it would not make much sense to use such a data which is not the same as the user has sent it. TCP makes sure such a scenario exists only in blogs and not in real life.
Problems TCP is designed to solve?
TCP is designed to majorly solve the following:
Reliability: TCP makes sure the data sent is reliable and your rest without worrying about data reaching the end it was intended to using the 3- way handshake.
Corruption Control: TCP makes sure the data sent isn’t corrupted.
Ordering: TCP keeps proper ordering of data, so that no matter in what order the receiver gets data, it can easily be arranged in the order that makes sense.
Flow Control: TCP makes sure it stops a fast sender from overwhelming a slow receiver.
Congestion Control: TCP keeps a watch or the network and reduces speed when there are signs of traffic jams.
Terms Related to TCP 3 ways Handshake
Before we discuss the working it would make it easier to know the definitions and meaning of a few terms that would be further used in the blog, you would be able to make more sense out of it once you see where these pieces fit in the TCP puzzle:
Sequence Number: Random number which helps to track data by marking the position of data so the receiver knows the order.
Acknowledgement Number: Indicates the next the receiver expects to receive.
Window Size: Specifies how much data the receiver can accept without acknowledgement.
Max Segment Size: Largest chunk of data the receiver can accept in one go.
SYN flag: Used to initiate a TCP connection and synchronise sequence numbers.
ACK flag: Indicates that the acknowledgement number field is valid.
FIN flag: Used to gracefully terminate a TCP connection when data transfer is complete.
TCP 3- way Handshake
TCP 3 way handshake is a process used used by TCP to start a safe and reliable connection between a client and server before any data is sent. TCP 3 way handshake is required because TCP transmits data in full duplex, which means both the sender and receiver can send and receive data simultaneously, so both sides must first agree on connection parameters like sequence numbers and readiness before data transfer begins.
The 3 way handshake comprises of 3 major actions:

SYN: In this the client initiates connection by sending a packet with SYN flag set suggesting that it wants a connection with the server
SYN - ACK: The server receives the SYN flag from the client and responds by sending back a packet with both SYN ACK flag set. This signifies the acknowledgement of client request and suggesting that its ready as well.
ACK: The client sends final ACK packet in acknowledgement of the server’s SYN-ACK and connection is established for data sharing.
How Data Transfer works in TCP?
Once TCP has successfully established the 3-way handshake, actual data transfer can begin, as both the client and server have mutually agreed on the connection parameters.
Data is not sent as a single continuous unit; instead, it is broken into smaller segments, where the Maximum Segment Size (MSS) defines the maximum amount of data that can be carried in each segment, making transmission more efficient and manageable.
Each TCP segment carries a sequence number that identifies the position of its data within the overall byte stream. This allows the receiver to correctly reassemble the data even if segments arrive out of order.

Upon receiving the data, the receiver sends an acknowledgement (ACK) containing the next expected sequence number along with a window size, which specifies how much additional data the receiver is capable of accepting without further acknowledgements.
How TCP ensures reliability, order & correctness?
TCP ensures reliable delivery using acknowledgements, timeouts and retransmissions.

When TCP sends data, it has already numbered every byte using sequence numbers, and the receiver sends ACKs indicating the next byte it expects. If the sender does not receive an ACK within a certain time, it assumes the packet was lost. The sender then retransmits the missing data and this process continues until all data is successfully acknowledged.
How TCP connection is closed?
After data has been successfully shared, it is time that the connection be terminated, for this TCP uses 4 way handshake.
Closing is more complex because TCP is full-duplex, which means each side closes independently. The flow in which closing takes place is:
FIN: Either side can send a FIN to close their end.
ACK-FIN: The side which earlier received FIN signal gives an acknowledgement of receiving it.
FIN: The other side now sends a FIN flag as well, indicating it is ready to terminate the signal as well.
ACK-FIN: The first side has also acknowledged the FIN flag from the other side and this results in termination of the connection.
Why not just 3-way close?
Instead of 3-way, TCP goes for 4-ways because:
One side may finish sending earlier.
The other side may still have data to send.
TCP Connection Life cycle

The TCP connection lifecycle begins with a three-way handshake that establishes a reliable, full-duplex connection between the client and server. Once the connection is established, data is transferred in ordered, numbered segments with acknowledgements ensuring reliability and retransmission of lost data. Finally, when communication is complete, the connection is gracefully closed using a controlled termination process, ensuring that all in-flight data is delivered before the connection is fully released.


