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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.