shutdown

shutdownSOCKET,HOW
This function shuts down a socket connection in the manner
indicated by HOW. If
HOW is 0, further receives are disallowed. If
HOW is 1, further sends are disallowed. If
HOW is 2, everything is disallowed.
shutdown(SOCK, 0); # no more reading shutdown(SOCK, 1); # no more writing shutdown(SOCK, 2); # no more I/O at all
This is useful with sockets when you want to tell the other side you’re done writing but not done reading, or vice versa. It’s also a more insistent form of close because it disables any copies of those file descriptors held in forked processes.
Imagine a server that wants to read its client’s request until
end-of-file, 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:
say SERVER "my request"; # send some data shutdown(SERVER, 1); # send eof; no more writing $answer = <SERVER>; # but you can still read
(If you came here trying to figure out how to shut down your
system, you’ll have to execute an external program to do that. See
system.)
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access