TCP Working: 3-Way Handshake & Reliable Communication
How Devices Synchronize and Guarantee Data Delivery.
Before learning about TCP or any protocol, we need to understand why it's necessary. If we transfer data or files without a protocol, the file or data packet can get lost, arrive out of order, or be duplicated. That's why TCP (Transmission Control Protocol) is important.
What is TCP and why it is needed?
Transmission control protocol is a internet protocol which ensures that communication between two device (sender and receiver) must be reliable over a network. TCP operates at 4th layer of OSI model.
Need of TCP:
Reliability: This protocol ensures data is not lost or corrupted during the delivery of data.
Ordered Data: During communication data is transferred in small data packets and every packet has a sequence number and TCP ensures that this packets must be received in a correct sequence order in which sender sends.
Connection Management: It uses 3 way handshake to establish a stable connection before sending connection.
Duplicate Data: TCP checks the duplicate data and discard duplicate packets.
Lost Packets: During the transfer if a data packet is lost TCP resends that data packets and manage the sequential order.
What is TCP 3-Way Handshake ?
3-Way Handshake is a 3 step process which include (SYN, SYN-ACK, ACK) used by TCP to establish a stable connection before transferring between sender and receiver.

Data packets or files sent between sender between and receiver is in the form of small chunks which is also known as segments.
Steps of 3-Way Handshake :
SYN(Synchronization): In first step of 3 way handshake process server sends data segment with request to establish a stable connection.
SYN-ACK (Synchronization - Acknowledgement): After receiving the segment and request SYN - ACK responds where ACK (Acknowledge) signify that segment is received or not and SYN(Synchronization) means the sequential number from this number segment will start.
ACK(Acknowledge): In the last step of 3-way handshake the sender acknowledges that the stable connection is established and ready to send data packets.

How data transfer works in TCP?
These are the following steps involved which shows how data transfer works in TCP(Transmission Control Protocol):
Connection establish: TCP establish a stable connection using 3-Way handshake which include:
SYN → Synchronization Signifies requesting to establish a stable connection.
SYN - ACK → Acknowledge + Agreeing that yes connection can be established.
ACK → Acknowledge that the connection is established between client and server.Data Segmentation: TCP sends data packets int small chunks called segments. Each segment contains a header which contain the sequence number responsible for reconstruction of order of packets.
Sequential Order: Every segment has an sequential order which helps in ordering of the segments.
Receiver acknowledges the sender that the package has arrived.
Example:
H E L L O
1 2 3 4 5 [Sequence Number]Ack 1
Ack 2
Ack 3 [ Lets assume packet three is lost, now it will be retransmit but still mange the sequence]
Ack 4
This is called Reliability.
- TCP keep the track and record of every send and receive of packets.
How TCP ensures reliability, order, and correctness?
A. Reliability (Data Must Arrive):
Uses ACK (Acknowledges) for every packet to ensure the delivery of every data packets.
In any case the packet is lost or not delivered that particular segment will be retransmitted.
Uses timeout to check the efficient receiving of the segments.
B. Order (Data Must Arrive in Correct order Sequence):
- Every segment has an sequence order which allows receiver to reorder the packets that arrived out of the order and detects missing or duplicate packets.
C. Correctness (Must not be a corrupted Data)
TCP calculates a checksum for each segment to detect corruption during transmission.
TCP uses a window size to manage the data transmission rate, ensuring the sender does not overloaded the receiver.
How a TCP connection is closed?
A TCP connection is closed. using a 4-way handshake, ensuring both sides independently finish sending data before terminating. The process involves:
Step 1: FIN (Client to Server): The client (or server) decides to close and sends a
FINsegment to the other party.Step 2: ACK (Server to Client): The server receives the
FINand sends anACKto acknowledge receipt of the closure request.Step 3: FIN (Server to Client): The server, after ensuring its data is sent, sends its own
FINsegment to the client.Step 4: ACK (Client to Server): The client sends an
ACKto confirm the server'sFIN.TIME_WAIT State: The client enters a
TIME_WAITstate for a specific period (often 1-2 minutes) to ensure the finalACKis received and to prevent old packets from interfering with new connections.
