Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

who first sets tcp FIN flag in client-server connection

Who should first set the TCP FIN flag. Server, when finished sending data, or Client, when received complete data?

regards

like image 286
BorisTheBlade Avatar asked Dec 07 '22 23:12

BorisTheBlade


2 Answers

FIN is sent when the application on that side requests that the connection be closed. This doesn't have to happen immediately after receiving a FIN from the other side, either - it's possible for one side to send a FIN, then the other side to send some more data before sending its own FIN.

Once the TCP connection is up, it's completely symmetrical - neither side is distinguishable as a "server" or "client". This implies that either side can send the first FIN. Since a host can send no more data on a connection after sending a FIN, it is usually the side that first knows that it has no more data to send that does so.

There is a good practical reason to design a network protocol so that the client is the one that first closes the connection - the "first closer" ends up in TIME_WAIT state for twice the Maximum Segment Lifetime, which is a few minutes. This occupies a small amount of resources for those minutes - but if this happens on the server side where the server is handling many hundreds of connections a second, those TIME_WAIT sockets will quickly add up. It's better to distribute this burden out among the clients.

like image 96
caf Avatar answered Dec 11 '22 11:12

caf


This depends on the application layer protocol. - from http://www.tcpipguide.com/free/t_TCPConnectionTermination-2.htm.

A TCP connection is normally terminating using a special procedure where each side independently closes its end of the link. It normally begins with one of the application processes signalling to its TCP layer that the session is no longer needed. That device sends a FIN message to tell the other device that it wants to end the connection, which is acknowledged. When the responding device is ready, it too sends a FIN that is acknowledged; after waiting a period of time for the ACK to be received, the session is closed.

In practice, it should usually be the client, but there is no reason why it must be so.

like image 24
Kimvais Avatar answered Dec 11 '22 10:12

Kimvais