develooper Front page | perl.beginners | Postings from April 2002

Socket closed connection

Bruno Figueira
April 1, 2002 04:59
Socket closed connection
Message ID:

How can I check if the remote peer closed my TCP connection?

The following code is OK (taken from the book) because it expects something
to come from the peer:

use IO::Socket;
$sock = new IO::Socket::INET (LocalHost => 'goldengate',
                                            LocalPort => 1200,
                                            Proto => 'tcp',
                                            Listen => 5,
                                            Reuse => 1
die "Socket could not be created. Reason: $!" unless $sock;

while ($new_sock = $sock->accept()) {
    while (defined ($buf = <$new_sock>)) {          # Will return undef if
peer closes connection!
        print $buf;

However, how can I check if peer closed when I am not expecting anything
from it?
There is not <$new_sock> in my code. I don't want to block on peer's input.

BrunoF Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About