short: close() in a child process does not close the socket. Why?
I have a problem with a server application that I'm writing.
I create a listening socket, wait for connections with select and once
there is an incoming connection I fork() and start exchanging data.
So far no problem. But when I try to close() the socket at the end of the
child process the socket does not really get closed.
close() returns no errors, yet the client application never notices that
the connection has been closed.
I have looked at the tcp/ip packets with tcpdump while debugging the
server/child process - when close() gets called in the child no FIN
packets are sent. I have also noticed that the FIN packets are sent only
once the _parent_ terminates.
To find out where the problem is coming from (threads or sockets) I have
removed the fork() and put the child's code directly into the parent. This
way close() works as it should.
One solution I have found is to use shutdown() instead of close() in the
child. This closes the connection right away as it should.
What I don't understand is why close()ing a socket in a child does not
work, while shutdown() does. Could someone please explain?
I'm writing this code on Linux, kernel 2.4.16 FWIW.