2

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
danglingpointer
  • 4,708
  • 3
  • 24
  • 42
PhiberOptixz
  • 512
  • 6
  • 13
  • Maybe you could shutdown it before (see man 2 shutdown). – Oliv Jan 18 '18 at 11:19
  • @Oliv I dont think you got my question right. It's about following the order of closure of FDs between the Parent FD & the clind FD. Let me know if you need further clarification – PhiberOptixz Jan 18 '18 at 11:23
  • So I do think that shutdown may helpfull for doing what you want since it actualy shutdown the connection, while closing a file descritor may not cause connection shutdown if the fd has been duplicated. It was just a remark and still not an answer to your question indeed. – Oliv Jan 18 '18 at 11:40
  • Possible duplicate of [Properly close a TCP socket](https://stackoverflow.com/questions/8872988/properly-close-a-tcp-socket) – Joel C Jan 20 '18 at 18:20
  • @joel C they are completely different questions though the intent might be the same – PhiberOptixz Jan 20 '18 at 18:31

1 Answers1

1

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.

Liam Kelly
  • 3,524
  • 1
  • 17
  • 41