TCP ( Transmission Control Protocol) - Session Initiation, transmission, and termination. 3-way and 4-way handshake
The Transmission control protocol (TCP) is a Transport layer protocol. This protocol ensures data transmission after establishing a session and the session can also end according to our needs.
To understand how TCP establishes a session we need to understand the TCP 3-way handshake.
TCP 3 WAY HANDSHAKE( SESSION ESTABLISHING )
SYN( Client to Server)
SYN-ACK( Server to Client)
ACK( Client to Server)
SYN( Synchronize Sequence Number) is sent from client to server telling the server that it wants to establish a session with it. It is basically a segment that informs the server about the sequence number from which it can send the segments. A segment is a unit of data in the transport layer as we studied in the OSI Model.
SYN-ACK is sent from the server to the client, accepting the session request by the client and also specifying the seq numbers that it would accept segments from.
ACK is finally sent by the client to the server and the session is established.
Now data transmission occurs from client to server.
After data transmission is done. The TCP session is terminated in a 4 WAY HANDSHAKE.
TCP 4 WAY HANDSHAKE( SESSION TERMINATION)
FIN ( Client to Server)
ACK ( Server to Client)
FIN ( Again Server to Client)
ACK ( Client to Server)
FIN segment is sent firstly by the client to the server. In this segment, the FIN bit is set to 1. Now the Client is in 1st wait state and waits for an acknowledgment by the server.
Now an ACK is sent by the server to the client and the client comes out of the 1st wait state and enters the 2nd wait state. After the server sends ACK. It also sends a FIN by setting the bit to 1 and this time when the client receives the FIN bit as 1 it comes out of the 2nd wait state and sends an ACK to the server again.
This is how a session termination happens.