Perl Cookbook

Perl CookbookSearch this book
Previous: 17.8. Finding Your Own Name and AddressChapter 17
Sockets
Next: 17.10. Writing Bidirectional Clients
 

17.9. Closing a Socket After Forking

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.

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

Discussion

When a process forks, the child has copies of all the parent's open filehandles, including sockets. When you close a file or socket, you close only the current process' 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 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 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 they 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 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

See Also

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


Previous: 17.8. Finding Your Own Name and AddressPerl CookbookNext: 17.10. Writing Bidirectional Clients
17.8. Finding Your Own Name and AddressBook Index17.10. Writing Bidirectional Clients