hello everyone. I tried searching for something related to this, but I figured it was time to ask my own question. I am experiencing these problems using Ubuntu 7.04
I am starting up a TCP listener/server and once connected, will act as a communication/control link with a program on another computer. In the final iteration I want it to run it like a daemon, disconnected from a controlling process so I am going to do:
result = fork()
if (result == 0){
//close stdio handles and reopen to /dev/null
setsig()
umask(0)
socket()
bind()
listen()
accept()
}
else
_exit(0)
This is simplified as i am using functions and setting socket options, particularily the SO_REUSEADDR option.
If I do NOT run the fork and just run the socket->bind->listen->accept I get the same problems.
The problem is:
The client connecting, whether it is my client, or even netcat will connect properly, but then the server immediately sends a disconnect. The server does not get out of accept() during this, and even stranger it is random, and sometimes it works (60%), and sometimes it doesn't (40%).
I could tell it connected and disconnected right away by using wireshark, I got a SYN -> SYN/ACK -> ACK and then right away FIN/ACK -> FIN/ACK -> FIN. Using netstat -ta also shows the ports getting gummed up in TIME_WAIT as though it did connect and disconnect.
What i think could be the problem
Two things I have done:
one is to not set the SO_REUSEADDR parameter, and it then works as expected, although I have not done as much testing because it requires you to wait for a minute after each test for the TIME_WAIT to clean up.
two, before I set up the socket, I read some parameters from a config file, by opening and closing it. If I comment this out and hard-code the port to listen on, etc, the occurance of it is MUCH less, although can still happen.
I am at my wits end with this. Is there anything I can do to figure this out or help debug it. I am pretty sure it has to do with file handles getting gummed up or something like that. But i have no Idea!
Thanks for reading!