PhiberOptixz
PhiberOptixz

Reputation: 542

Closing TCP Server gracefully on Linux system

After the creation the server socket we get the parent FD. Once the parent FD is used to accept the client(s) connections it will create further child FD(s).

So when calling the close() what is the best practice:

  1. Call the close() on all the client FDs, then call the close() on the parent FD.
  2. Call the close on the parent FD first, then call close() on each the child FDs
  3. Call the close() on the parent FD is enough

Upvotes: 2

Views: 942

Answers (1)

Liam Kelly
Liam Kelly

Reputation: 3704

Taking the web server definition of graceful closing and boiling it down to just TCP, you would get the following:

  1. Closing all listening/parent sockets but keeping all active connections running. This prevents new connections from forming.
  2. Waiting for the clients to close the active/child connections naturally, and close their sockets if they do.
  3. If after a given period of time there are still active/child connections, close their sockets forcefully.

Basically, you are giving the priority to the existing active connections and trying to forcefully close the least amount of them.

Upvotes: 1

Related Questions