in reply to Checking if a non-blocking socket is active
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Checking if a non-blocking socket is active
by bucz (Novice) on Oct 11, 2009 at 19:49 UTC | |
|
Re^2: Checking if a non-blocking socket is active
by desemondo (Hermit) on Jul 03, 2012 at 12:19 UTC |