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

Socket closed connection

From:
Bruno Figueira
Date:
April 1, 2002 04:59
Subject:
Socket closed connection
Message ID:
31E38B53D182D51195FA00508BE3A334A30DB6@zwnbc004.cala.nortel.com
Guys,

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




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About