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 just stop sending data and inform the other side about the connection termination? Yes this is possible, but TCP is a reliable protocol (reliable means no message loss at any stage), so 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 stream 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. Now, 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.

  • At the protocol level, a disconnect request is conveyed in the TCP FIN packet.
  • 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.

In the following sections, we will see, how a protocol flow achieves TCP connection termination.

There are 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 in which the connection needs to be close 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 closes the connection in both directions immediately.  The user application is also informed about the reset so that the application is aware that 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 there is connection termination or reset, the application gets an indication for the event. But How?  We will discuss the exact code for the C program for TCP sockets. Here will be just mentioning a way to detect the event. 

In socket programming, the server blocks on 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 the zero, means the connection is terminated by the peer with a FIN and FIN Ack.
  • For connection terminated by RST and RST-Ack,  the recv() system call, returns -1, and the errno is set to 104. 

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