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
When a process forks, the child has copies of all the parent's open filehandles, including sockets. When you
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
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
unused filehandles after a
, or use
function is a more insistent form of
- 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
lets you specify which sides of the connection are closed. An argument of
says that we're done reading data, so the other end of the socket will get a
if they try writing.
says that we're done writing data, so the other end of the socket will get an end-of-file if they try reading.
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
, that socket is now invalid for I/O, so no answer would ever come back. Instead, the client should use
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