17.9. Closing a Socket After ForkingProblem
Your program has forked and you want to tell the other end that you're done sending data. You've tried Solution
Use shutdown(SOCKET, 0); # I/we have stopped reading data shutdown(SOCKET, 1); # I/we have stopped writing data shutdown(SOCKET, 2); # I/we have stopped using this socket On an IO::Socket object, you could also write: $socket->shutdown(0); # I/we have stopped reading data Discussion
When a process forks, the child has copies of all the parent's open filehandles, including sockets. When you Take the case of a socket that data is being sent to. If two processes have this socket open, one can close it but the socket isn't considered closed by the operating system because the other still has it open. Until the other process closes the socket, the process reading from the socket won't get an end-of-file. This can lead to confusion and deadlock.
To avoid this, either
The numeric argument to
Imagine a server that wants to read its client's request until end of file, and send an answer. If the client calls print SERVER "my request\n"; # send some data shutdown(SERVER, 1); # send eof; no more writing $answer = <SERVER>; # but you can still read See Also
The
|
|