What is a TCP Connection Termination procedure?

TCP is an example of a connection-oriented protocol in computer networks. Before sending any data, the communicating computers set up a dedicated both-way connection.  

The procedure is named TCP three-way handshake. In this tutorial, we are describing the connection termination procedure with examples.

The first thing that comes to mind is why there is a procedure for connection termination. A communicating entity can stop sending data and inform the other side about the connection termination. This is possible, but TCP is a reliable protocol (reliable means no message loss at any stage), so it defines a protocol procedure to terminate the connection with no message loss.

A TCP connection is a pair of unidirectional streams, one stream in each direction. E.g., if hosts A and B have a TCP connection in between, one stream is from A to B, and the other is from B to A. Each side of the stream should be closed gracefully. Let’s try to understand the need for a connection termination procedure in layman’s terms.

How does a connection terminate? With example –

TCP connection termination

Assume two people are conversing on the phone. The voice ebbed and flowed in all directions. Before terminating the call, each party checks to ensure that all spoken words have been relayed to the other party.

A final bye statement is used to conclude the conversation. In TCP, a communicating end indicates to the other that it has no more data to send and requests that the connection be closed.

  • The TCP FIN packet conveys a disconnect request at the protocol level.
  • Upon receiving a close request from the TCP user. The TCP layer stops sending new packets and waits for the pending TCP acks. Once all pending packets are transmitted successfully, the sender sends the TCP FIN to the receiver.
  • Now the other ends do the same.

The following sections will show how a protocol flow achieves TCP connection termination.

Two packets, TCP FIN and TCP FIN ACK, are used for connection termination. Here we will discuss each packet in detail.

TCP FIN and TCP Fin Ack packets:

The sender sends TCP FIN to the receiver for an outgoing stream. The packet has a FIN flag set as another type of TCP message. The packet has a sequence number. The receiver sends the FIN Ack with one more sequence number received in the FIN. Now the connection is closed in one direction. At this stage, the application is informed about the close of the connection.

For another direction, a new fin is sent with a sequence number. The receiving side sends the fin ack, and the connection is closed in both directions.

TCP Connection reset (RST, RST Ack)

Above, we have discussed the graceful TCP connection termination. But there are situations where the connection must be closed or reset immediately. This may be because of system errors or protocol errors.

TCP connection reset, RST ACK
TCP connection reset, TCP RST, TCP ACK

For example, a TCP end receives a packet for which there is no connection. The receiving side will send a TCP RST to the remote to close the connection and again set up if required. The other ends send the TCP RST Ack. In contrast to the FIN, RST, and RST, Ack immediately closes the connection in both directions. The user application is also informed about the reset so that the application knows there can be packet loss and will take action accordingly.

TCP connection termination or reset indication to the TCP user:

Till now, we have discussed all protocol messages, but nothing completes without discussing the events handling in the user application. Whenever connection termination or reset occurs, the application gets an indication of the event. But How? We will discuss the exact code for the C program for TCP sockets. There will be just mentioning a way to detect the event. 

In socket programming, the server blocks the recv() system call to read the TCP message from the client. Upon a message from the client, the recv( ) function returns the number of bytes read.

  • If it returns zero, the peer terminates the connection with a FIN and FIN Ack.
  • For connection closed by RST and RST-Ack,  the recv() system call returns -1, and the errno is set to 104. 

The connection is closed in both cases, but the application gets the reason and can take action accordingly.