I wrote this small server application in pure C that listens to incoming connections in a given port, very simple stuff.
It goes with the usual socket initialization procedure, create the socket()
then bind()
to the port, says its a listen()
, and ifinitely loops through a select()
waiting for incoming connections to accept()
.
All goes just fine and works like a charm, except that if I leave the thing running for a couple months, the listening port closes while the application server keeps running unaware of it, since I wrote it to trust the listening socket will not close if not told to.
So the question is: Why the hell is the port being closed without my application's concern and what can I do to prevent it from happening?
Is that expected behaviour? Should I check for some kind of exceptions or make "health check" on the listening socket to reopen it if necessary?
Code: https://gist.github.com/Havenard/e930be035a3bee75c018 (yes I realize I'm using 0
as cue for errors and it's bad pratice and stuff, but it is not relevant to the question as I explained in the comments, when I set the socket file descriptor to 0
it is to stop the loop and shut down the application).
'if (skt_accept == 0)... )
try to use -1 as invalid filedescriptor (and set them to -1 when invalid) 4) in your gobackground() function you do a printf after closing filedesctiptors 0,1,2. 5) you really should ignore some of the -1 returns from select(), such AS EAGAIN. 6) The linked lists are messy and overly complicated, IMHO. 7)' '
is more readable than 0x20 – wildplasser