in reply to Detecting a closed socket

Hi!

I have written servers in perl, and the following works for me, to determine whether a socket is closed:

As you said, IO::Select will indicate that the socket can_read, so you'll go ahead and read from it, using sysread. sysread returns the number of bytes that were read, or undef on error, or 0 if end-of-file was reached. That last thing basically means that the socket was closed - or at least, you're not going to read from it anymore. So, your logic will look like the following untested, pseudo-code snippet:

my $bytes_read = $socket->sysread($buf, $max_read_len); if (not defined $bytes_read) { print "ack! error on the socket\n"; } elsif ($bytes_read == 0) { print "the socket was closed\n"; } else { print "woo hoo! I read $bytes_read bytes from the socket...\n"; }

Hope that helps.

--
3dan