TCP:working
Imagine you are sending a long message on whatsapp to your friend .The message that you sent to your friend ,he got some message twice and some of the message not reached to him ,some of the part of the message reached after the very much gap of time.These all things happen due to their is no proper formate/rule for the transfer of message.So some rules are made which we know Protocol.
What is TCP
Transmission control protocol (TCP) is a communication protocol which is connection oriented protocol that ensure data should be sent in correct order.Before transfer data it fixed the path.
Why
Because to transfer data without any order mismatch and data packet should be sent properly, so we need TCP.
Reliability: It ensures data is delivered without errors.
Ordered Delivery: It delivers data in the correct order.
Connection Management: It establishes a connection before data transmission to ensure a stable, dedicated session.
e.g:
loading a web browser.
receiving and sending Email.
Problem solved
It transfer data in correct order.
If any data may be loss then it sent again to the destination.
if their is any duplicate packet then it discard them.
It also control congestion of the network.
TCP-3 way Handshake
TCP does something before transfer the data from sender to receiver.It established a reliable ,connection oriented session between sender and receiver to send data.It cannot send data blindly without connection.
It ensure that both are ready then send data in synchronized sequence.

Step-by-Step Working of the TCP Handshake
Step 1: SYN (Synchronize) - Client to Server
The client wants to establish a connection, so it sends a TCP packet to the server with the SYN .
Step 2: SYN-ACK (Synchronize-Acknowledgment) - Server to Client
The server receives the SYN packet. It responds with a packet that has both the SYN and ACK .
Step 3: ACK (Acknowledgment) - Client to Server
The client receives the SYN-ACK packet from the server.
The client sends a final ACK packet to the server.
How data Transfer works
Firstly ,TCP break the large dataset into smaller segments and adds a header with a sequence number .
Both device is connected then send and receive data simultaneously.
After getting the whole data receiver sends ACK within the certain time .If the ACK is not came then it thought data is missing and start sending again.
How TCP ensures reliability, order, and correctness
Reliability :
The receiver sends an ACK back to the sender for each packet received.
If a packet is lost then it automatically resends data.
Ordered Delivery :
Each segment is assigned a unique number which allow the receiver to reorder packets that arrive out of sequence.
Correctness (Error Checking):
TCP calculates a checksum for each segment.
If the receiver detects a mismatch then the segment is discard .
How TCP connection Closed
TCP is closed by the 4-way handshake.In this FIN(finished)packet part is increased because It is a full duplex .So ,closing is also necessary.
The 4-Way Handshake Process
FIN (Active Close): One party decides to terminate the connection and sends a FIN packet to the other side.
ACK (Passive Close): The receiver receives the FIN and sends an ACK to acknowledge receipt. The server may continue sending data at this stage.
FIN (Passive Close): Once the server has finished sending its own data, it sends its own FIN packet to the client.
ACK (Active Close): The client receives the FIN and sends an AKN to confirm the final shutdown.