I suppose that HERE I should check for some disconnect error,
Disconnection is not an error. A premature/unexpected disconnection might leave the program with partial or invalid data, but sysread doesn't care about that. It's up to the program to detect and handle that situation.
Disconnection is signaled as EOF would be for a file: Read from the socket and check if sysread returned zero (as opposed to undef). This applies to both blocking and non-blocking sockets.
For example,
use Errno qw( EAGAIN EINTR ); my $rv = sysread($sock, $buf, $BLKSIZ, length($buf)); if (!defined($rv)) { if ($! == EAGAIN) { # Only reached you are using a non-blocking socket. # No data available at the moment. Try again later. } elsif ($! == EINTR) { # Only reached if you have a signal handler. # Interrupted by signal. Try again. } else { # Error! } } elsif (!$rv) { # EOF. Socket was closed else { # Data was read }
For blocking sockets, you can check whether sysread will block or not using IO::Select. For a closed socket, it will indicate the socket is readable.
Update: I originally understood "blocking" when you said "non-blocking". I adjusted my post to compensate.
In reply to Re: Checking if a non-blocking socket is active
by ikegami
in thread Checking if a non-blocking socket is active
by bucz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |