So do a second read. Nothing guarantees you'll get the number of bytes you've requested.
use Carp qw( croak ); sub sysread_full my $fh = $_[0] our $buf; local *buf = \$_[1]; # alias my $to_read = $_[2] my $offset = $_[3] || 0; $offset += length($buf) if $offset < 0; croak("Offset outside string") if $offset < 0; my $tot_read = 0; while ($to_read > 0) { my $bytes_read = sysread($fh, $buf, $to_read, $offset+$tot_read); return undef if !defined($bytes_read); return $tot_read if !$bytes_read; $to_read -= $bytes_read; $tot_read += $bytes_read; } return $tot_read; } ... sysread_full $sock, $read_length, 20; sysread_full $sock, $buffer, $read_length; ...

The behaviour I've observed follows: If there are bytes available to be read, they will be returned immediately, even if there are fewer than requested. (Users of select rely on this.) If there no bytes available to read, it will block.

Update: Added code.
Update: sysread_full differed from sysread in its handling of the offset. Fixed.


In reply to Re: Missing data in IO::Socket::INET sysread/syswrite by ikegami
in thread Missing data in IO::Socket::INET sysread/syswrite by reasonablekeith

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.