Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Detecting a closed socket

by edan (Curate)
on Aug 05, 2003 at 07:11 UTC ( #280904=note: print w/replies, xml ) Need Help??

in reply to Detecting a closed socket


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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://280904]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (1)
As of 2021-06-19 09:25 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (91 votes). Check out past polls.