Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    How to terminate a socket server gracefully?

    I have a server using unix domain socket. It works in a standard way: listening for requests and accepting each of them by running a separater thread for it:

    int clientsocket;

    clientsocket=accept(serversocket, NULL, NULL);

    ClientHandler * ch = new ClientHandler(someparameters, clientsocket);

    So, how to terminate this server gracefully? I think some signal handler can be used for this purpose though I haven't done so and it's ok to do "kill -TERM pid" w/o the signal is handled by the server. But since there is a lot of cleanning work to do for each socket connection, this is not something recommended.

    Any idea or suggestions? Thanks!

  2. #2
    Linux User cyberinstru's Avatar
    Join Date
    Jan 2007
    It is always better to trap signals and close the socket. Otherwise, the opened socket would be left unclosed and if your system reaches the max number of open descriptors limit, u wud be able to accept any new connections.
    Registered Linux User #440311

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts