TCP Connection Termination
TCP is a connection oriented protocol. It is used for reliable communication between sender and receiver. TCP three-way handshake (SYN, SYN-ACK, and ACK) is used to establish connection for data transmission. In this post, we will understand scenario where a TCP connection between client and server is already established and now client wants to terminate connection and stop data transmission. Following steps are performed to terminate connection –
TCP Connection Termination
The connection termination phase uses a four-way handshake, with sender and receiver side of the connection terminating independently. When an endpoint initiates to stop its connection, it sends a FIN packet, and other end acknowledges with an ACK packet. A graceful shutdown requires a pair of FIN and ACK segments from each TCP endpoints. After both FIN/ACK exchange, the side which sent the first FIN, before finally closing the connection. A connection can be half-open where one side has terminated from its end but the other has not. The side that has terminated can no longer send any data into the connection but the other side can send. The terminating side should continue reading the data until the other side terminates the connection as well. Termination of connection is a pair of 2-way handshake, when host A sends a FIN and host B replies with a FIN & ACK (merely combines 2 steps into one) and host a replies with an ACK. This is perhaps the most common method. It is possible for both sides to send FIN simultaneously, then both just have to ACK. This is considered a 4-way handshake since the FIN/ACK sequence is done in parallel for both directions.
Related – TCP FIN VS RST Packets
SIDE A | SIDE B | ||||
START STATE | ACTION | TRANSITION TO STATE | START STATE | ACTION | TRANSITION TO STATE |
ESTABLISHED | The Side A TCP sends a segment with the FIN bit set to request that the connection to be closed. | FIN-WAIT-1 | ESTABLISHED | At this stage Side B is still in normal operating mode. | —- |
FIN-WAIT-1 | Side A sent a FIN, is waiting for it to be acknowledged and waits to receive FIN from side B. In this state Side A can still receive data from Side B but will no longer accept data. | —– | ESTABLISHED | Side B receives Side A’s FIN. It sends an ACK to acknowledge the FIN. Side B must wait for the application using it to be informed the other end is closing. | FIN-ACK
|
FIN-WAIT-1 | Side A receives the ACK for its FIN. It must now wait for Side B to close. | FIN-WAIT-1 | FIN-ACK | Side B waits for the application process on its end to signal that it is ready to close. | CLOSE-WAIT |
FIN-WAIT-2 | Side A is waiting for Side B’s FIN. | —- | CLOSE-WAIT | Side B TCP receives notice from the local application that it is done. Side B sends its FIN to Side A. | LAST-ACK |
FIN-WAIT-2 | Side A receives Side B’s FIN and sends back an ACK. | TIME-WAIT | LAST-ACK | Side B is waiting for an ACK for the FIN it sent. | FIN-WAIT |
TIME-WAIT | Side A waits for a period of time equal to double the maximum segment life (MSL) time, to ensure the ACK it sent was received. | TIME-WAIT | FIN-WAIT | Side B receives the ACK to its FIN and closes the connection. | CLOSED |
TIME-WAIT | The timer expires after double the MSL time. | CLOSED | CLOSED | The connection is closed on Side B’s end. | |
CLOSED | The connection is closed. | CLOSED | The connection is closed. | ||
Download the table here.
TCP Connection reset (RST, RST ACK)
TCP connection termination with FIN and FIN-ACK is graceful termination of connection. But there are certain situations in which connection needs to be closed or reset immediately. This may be because of a system error or protocol error. TCP end receives a packet for which there is no connection. Receiver will send a RST to the remote host to close the connection and again setup if requires. The remote host sends the TCP RST ACK. As compared to the FIN and FIN-ACK, RST and RST ACK closes the connection in both the directions immediately. TCP user application also informs about the reset, so application is aware that there can be packet loss and will take actions accordingly.
ABOUT THE AUTHOR
I am here to share my knowledge and experience in the field of networking with the goal being – “The more you share, the more you learn.”
I am a biotechnologist by qualification and a Network Enthusiast by interest. I developed interest in networking being in the company of a passionate Network Professional, my husband.
I am a strong believer of the fact that “learning is a constant process of discovering yourself.”
– Rashmi Bhardwaj (Author/Editor)