TCP Working: 3-Way Handshake & Reliable Communication

When cleint made some query from server, than what you think actually happens? Like how server agrees to call and fulll fill query and how client get its response back? No thought, Don't worry at the end of this blog you will have a complete picture in your mind.
Well moving ahead with my question, let’s assume a scenrio of a post office, but it does not have a proper rule system or protocols. No data handling for incoming and outgoing posts.
Don't you think that there must be some sort of connection establishment between server and client and to make this connection there must be some sort of protocol that will be followed, Yes?
TCP(Transmission Control Protocol) is one of those connection oriented network protocols that followed during this connection establishment. To avoid overloading, it breaks data into small packets.
Lets take a practical scenario, suppose you have to send some packets from point A to point B, what problems you might face or services expect from middleman, let's list them out:
1.) Reliability - Middleman via which you are sending actually carrying them from A to B.
2.) Ordering - Packets reach in same order to B as sent from A.
3.) Loss Detection - If by chance some packets gets missed then there must be a record that which were missed.
4.) Retransmission - If some packets gets destroyed in between or lost then again start transmission for missing packet.
5.) Flow Control - Rate of transfer must be suitable for receiver and not overwhelm it.
6.) Congestion control - And to keep communication efficient, avoid network congestion.
This bare minimum is expected while transferring of some data. TCP gives all these features and solves our problem. How ?
3- way Handshake: This is the core idea which makes TCP that reliable. Before start sending data, server and client perform 3-way handshake.
Like on a normal call of 2 peoples how it goes?
Person-1 : “Hi”
Person-2 : “Hi, Am i audible”
Person-1 : “Yes”
Person-2 : “Perfect, let’s start the conversation.”
This is what server and client do while performing handshake.
How this 3-way handshake works ?
1.) SYN (Synchronize): The client initiates the connection by sending a TCP segment with the SYN flag set to 1, a random initial sequence number (x), and sometimes the Maximum Segment Size (MSS).
2.) SYN-ACK (Synchronize-Acknowledgment): The server receives the SYN, sets the SYN and ACK flags to 1, and responds with its own random sequence number (y) and an acknowledgment number set to the client's sequence number plus one (x+1).
3.) ACK (Acknowledgment): The client acknowledges the server's response by sending an ACK segment, setting the acknowledgment number to the server's sequence number plus one (y + 1).
Data Loss and Retransmission
Let’s understand how TCP is able to track order of data packets and catch packet loss.
Sender’s TCP when breaks data in small chunks, it assigns these packets a sequence numbers used to maintain their order and checks for lost packet. And report to reciever’s side the total segment size and when packets reached to reciever’s TCP it checks there sequence number and if some sequence number is found to be missing,the receiver then sends an acknowledgment requesting retransmission of the missing packet.
Now in a good communication, a handshake is made in start and to disconnect in a good manner a handshake will be also made while terminating the communication. Unline 3 way while establishing, while terminating the connection 4-way handshake is performed.
1.) FIN-ACK 1: Indicating that there is no more data to send, sender sends a FIN to the reciever to initiate closure (often also containing an ACK for last received data).
2.) ACK 1: Reciever receives the FIN and replies with an ACK.
3.) FIN-ACK 2: Reciever sends its own FIN to initiate closing its side.
4.) ACK 2: Sender acknowledges the reciever’s FIN, closing the connection (Time-Wait state)
And this way TCP becomes reliable and widely used protocol to tranfer data. Hope i made this picture clear in your mind✌️.
