In TCP Connection Terminationlesson, you will learn how Transmission Control Protocol (TCP) terminates an established connection gracefully.

Transmission Control Protocol (TCP) Connection Termination

When the data transmission is complete and the device want to terminate the connection, the device initiating the termination, places a TCP segment (Segment is the name of the data packet at transport layer, if the protocol is TCP) with the FIN flag set to one. The purpose of FIN bit is to enable TCP to gracefully terminate an established session. The application then enters in a state called the FIN-WAIT state. When at FIN-WAIT state, Device A continues to receive TCP segments from Device B and processes the segments already in the queue, but no additional data is accepted from the application.

TCP Connection Termination

TCP Connection Termination

In the example shown above, assume Device A has completed its transmission and indicates this by sending a segment to Device B with the FIN bit set to 1. Device B will acknowledge the segment with an ACK. At this point in time, Device B will no longer accept data from Device A. Device B can continue to accept data from its application to transmit to Device A. If Device B does not have any more data to transmit, it will also terminate the connection by transmitting a segment to Device A with the FIN bit set to 1. Device A will then ACK that segment and terminates the connection.

Cited from: