Checking whether a socket has been closed by the peer

J

Jakob Lell

Hello,
how can I check whether the client has closed an IO::Socket::INET?
According to the description, the connected-Method of IO::Socket should
return undef if the socket isn't connected. However, this doesn't work
when the peer has closed the connection. When you run the script below
and connect to port 1234 from another terminal using netcat or telnet and
then close the connection, the script will continue to write "connected\n"
to STDOUT every second.

use IO::Socket;
my $sock=IO::Socket::INET->new(LocalPort=>1234,Listen=>5,ReuseAddr=>1)||die $!;
while(my $client=$sock->accept()){
next unless defined($client);
$client->blocking(0);
while(defined $client->connected){
print "connected\n";
sleep(1);
}
}

Is there any way to check whether the connection has been closed by the
peer? I've already tried eof and the has_exception method from
IO::Select, but neither solved my problem. I know I could try to read
anything from the socket but as I'm using nonblocking IO, this is no
solution for the problem.

I'm using Linux 2.6.5 and Perl 5.8.3.

Regards
Jakob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top