What is tcp connection termination ?

TCP is an example of connection oriented protocol in computer networks. In other tutorial for tcp three way handshake , we have  shown the connection setup procedure in tcp. This tutorial describes about the connection termination procedure in detail with the examples.  First thing comes in mind why there is a procedure for connection termination ? Any node just stop sending data and inform to the other node about the connection termination? Yes this is possible, but TCP is a reliable protocol (reliable means no message loss), so requires a protocol procedure to terminate the connection which 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 other stream is from B to A. Each stream should be closed gracefully. Lets try to understand the need for connection termination procedure in layman’s term.

tcp connection termination
tcp connection termination

Suppose two person talking to each other over phone. The voice flowing each the direction. Now before ending the call, each person make sure that all words are reached to another person. A final bye statement is used. In tcp it is similar , an sending tcp node says to the receiving node that , i have no more data to send and request to close the stream. At protocol level this is conveyed in TCP FIN packet. Upon receiving a close request from the tcp user.  TCP layer stops sending new packets and wait for the pending tcp acks. Once all pending packets are transmitted successfully, sender sends the TCP FIN to the receiver. Now other end do the same.

In 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 a outgoing stream. The packet have FIN flag set as like another type of TCP messages.  The packet have 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 state the application is informed about the close of connection.

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

 

TCP Connection reset (RST, RST Ack)

Above we have discussed about the graceful TCP connection termination. But there are situations in which connection needs to be close or reset immediately. This may be because of a system errors or protocol errors.

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

For example, a TCP ends receives a packet for which there is no connection. Receiving side will send a TCP RST to the remote, to close the connection and again setup if requires.  The other ends sends the TCP RST Ack. In contrast to the FIN , RST and RST Ack closes the connection in both the directions immediately.  The TCP user application also informed about the reset, so that application is aware that there can be packet loss and will take actions accordingly.

TCP connection termination or reset indication to the tcp user:

Till now we have discussed all about protocol messages , but nothing completes without discussing about the events handling in user application. Whenever there is connection termination or reset, application gets an indication for the event. But How ?  We will discuss about 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 recv() system call to read the tcp message from client. Upon a message from client, recv( ) return the number of bytes read. If it returns the zero , means connection is terminated by the peer with a FIN and FIN Ack. For connection terminated by RST and RST Ack,  recv system call , return -1 and errno is set to 104.  It both the cases connection close , but application gets the reason and can take actions accordingly.

 

Leave a Reply

Your email address will not be published. Required fields are marked *