Recipe 17.9 Closing a Socket After Forking

17.9.1 Problem

Your program has forked and you want to tell the other end that you're done sending data. You've tried close on the socket, but the remote end never gets an EOF or SIGPIPE.

17.9.2 Solution

Use shutdown:

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

17.9.3 Discussion

When a process forks, the child has copies of the parent's open filehandles, including sockets. When you close a file or socket, you close only the current process's copy. If another process (parent or child) still has the socket open, the operating system doesn't consider their file or socket closed.

Take the case of a socket that data is being sent to. If two processes have this socket open, one can close it but, because the other still has it open, the socket isn't considered closed by the operating system. 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 close unused filehandles after a fork or use shutdown. The shutdown function is a more insistent form of close it tells the operating system that even though other processes have copies of this filehandle, it should be marked as closed, and the other end should get an end-of-file if the processes read from it or a SIGPIPE if they write to it.

The numeric argument to shutdown lets you specify which sides of the connection are closed. An argument of 0 says that we're done reading data, so the other end of the socket will get a SIGPIPE if they try writing. 1 says that we're done writing data, so the other end of the socket will get an end-of-file if they try reading. 2 says we're done reading and writing.

Imagine a server that wants to read its client's request until end-of-file, and then send an answer. If the client calls close, that socket is now invalid for I/O, so no answer would ever come back. Instead, the client should use shutdown to half-close the connection.

print SERVER "my request\n";        # send some data shutdown(SERVER, 1);                # send eof; no more writing $answer = <SERVER>;                 # but you can still read

17.9.4 See Also

The close and shutdown functions in Chapter 29 of Programming Perl and in perlfunc(1); your system's shutdown(2) manpage (if you have it)



Perl Cookbook
Perl Cookbook, Second Edition
ISBN: 0596003137
EAN: 2147483647
Year: 2003
Pages: 501

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net