TCP Working: 3-Way Handshake & Reliable Communication
What is TCP and why it is needed?
TCP stands for Transmission Control Protocol. It is a set of rules which is used to send data over the internet.
It is needed because if we don't have any predefined rule for sending the data, then it would not be possible to find if the data has arrived correctly or not. If it is arrived then the data packets(data is broken into small packets and sent) are in correct order or not. Senders and receivers won't know when to start and when to stop. Many problems like this will exist.
Problems TCP is designed to solve
TCP is designed to solve the four core problems:
Packet loss: While sending the data packets, some packets may get lost. TCP has mechanism to find which packet got lost.
Out-of-order delivery: Packets may arrive in random order. TCP arranges the packet in correct order.
Duplicate packets: A packet may arrive twice to the receiver but TCP verifies, if it is a duplicate packet then TCP ignores it.
No start or end clarity: Without TCP, both sender and receiver does not have clarity when to start sending the data and when to stop.
What is the TCP 3-Way Handshake
TCP 3-way handshake is something like attending online meeting. In the very start of the meeting, person A asks, "Am I audible?". To which person B replies, "Yes you are audible. Am I audible?". To which person A replies, "Yes you are also audible?". This way we confirm the connection. This is how TCP also establish a connection.
Step-by-step working of SYN, SYN-ACK, and ACK
TCP 3-way handshake:
step 1: client -> server
- Client initiates a connection by sending a TCP segment in which the SYN(Synchronize) flag is set to 1 and a random initial sequence number(let say X).
step 2: server -> client
- Server replies to the client with SYN and ACK(Acknowledgement) flag set to 1. Sever acknowledges the client by sending an ACK number which one more than the sequence number sent by the client(ACK = X+1) along with its sequence number(lets say Y).
step 3: client -> server
- Client acknowledges the server by sending an ACK number which is one more than sequence number sent by server(ACK = Y+1). Once it is received by the client, the connection is established.
How data transfer works in TCP?
Once the 3-Way handshake is done, then the client and servers can start sending data.
TCP breaks the data in small data packets numbers them and send them to the server.
After receiving every packet, the server sends an acknowledgement to the client.
How TCP ensures reliability, order, and correctness?
Since the receiver sends an acknowledgement for the every packet it received, client can find out if any packet got lost if client doesn't receives acknowledgement for that packet. This ensures it TCP's reliability.
When the packets were sent to the receiver it was already numbered so TCP ensures the packets are in order when they are received by the reciver.
If a packet arrives twice then TCP detects the same packet number and discards the packet, ensuring the correctness of data received.
How a TCP connection is closed?
TCP connection is closed in 4-way handshake:
step 1: client -> server
- client sent a FIN packet, which tells the server that client has done with sending the data and wants to close the connection.
step 2: server -> client
- server acknowledges the client by sending and ACK packet to the client. This way the connection is half-closed. Server can still send data to client.
step 3: server -> client
- Server sends a FIN packet, which tells the client that server is done with sending data and wants to close the connection.
step 4: client -> server
- Client acknowledges the server by sending and ACK packet. This way the connection is closed.