Click to See Complete Forum and Search --> : Elegant shutdown of server when signal is received


verbal
June 25th, 2009, 03:08 PM
Hi all,
I have built a TCP server which i need to shutdown nicely when some signals are received. My problem is this:
As a first approach i though of setting a global variable which would then cause the server to exit. But most of the times the server is blocked in the accept() call when the signal arrives so it is needed for one additional client to connect in order for the close to take effect. So now i throw an exception from the signal handler which is then causing the server to shutdown. Using exceptions however from within a signal handler is strongly discouraged as it may cause undefined behavior. Is there some more elegant solution for shutting down the server?
Thanks in advance,
verbal

Codeplug
June 25th, 2009, 09:26 PM
Call select() on the listening socket (read set). Then you can specify a timeout so that you can check for other conditions periodically - like time to quit.

gg

Richard.J
June 26th, 2009, 12:52 PM
or close the listening socket and accept() should return an error.