Reputation: 11
I'm currently working on a Linux TCP/IP server. The server program is running in C. I'm currently testing it, but each time I exit it with Ctrl-c, the port it's using is not released, neither is the database it's been writing to unlocked. How does one define a subroutine that will exit when a Ctrl+C signal is received?
Upvotes: 1
Views: 2486
Reputation: 13983
Two options:
As R pointed out, sigaction is more portable than signal, but perhaps less idiomatic for Linux.
Warning: atexit routines won't run if your program is killed with SIGKILL (Ctrl+/) or any other unhandled signal is received.
Upvotes: 1
Reputation: 5274
@Bortds Generally the port won't be released immediately, you have to wait to for some time. I found this from a server project I worked.
Upvotes: 1
Reputation: 215457
Lookup setsockopt
and SO_REUSEADDR
. This option must have been set on the old original server process's socket or the new one will not be able to bind the port until the TIME_WAIT
period expires.
Upvotes: 1