Actually, what I was thinking of in my previous reply is sysreadread, OTOH, does actually seem to block until the requested number of characters are read, or eof is reached (this is apparently due to the buffering that read involves, as opposed to sysread).  At least that's what a quick test shows.

Here's a simplified demo that (kind of) does what I understood you want to do:

Server:

#!/usr/bin/perl -w use strict; use IO::Socket::INET; my $server = IO::Socket::INET->new( LocalHost => 'localhost', LocalPort => 12345, Proto => 'tcp', Listen => SOMAXCONN, Reuse => 1 ) or die $!; print "server started\n"; $SIG{PIPE} = 'IGNORE'; while (1) { my $client = $server->accept(); print "accepted connection\n"; $client->autoflush(1); LOOP: for (1..1000) { my $count = int(rand 3)+1; # 1..3 32-bit BE uints, preceded by the respective count my $data = pack "N"x($count+1), $count, ($_)x$count; for (split //, $data) { last LOOP unless print $client $_; select undef, undef, undef, 0.01; # emulate slow delivery } } close $client; }

Client:

#!/usr/bin/perl -wl use strict; use IO::Socket::INET; my $sock = IO::Socket::INET->new("localhost:12345") or die $!; my $buf; while ($sock->read($buf, 4)) { # get count my $count = unpack "N", $buf; $sock->read($buf, $count*4); # read actual data my @data = unpack "N$count", $buf; print "$count: @data"; } close $sock;

Client output:

$ ./888339_c.pl 3: 1 1 1 3: 2 2 2 1: 3 1: 4 1: 5 3: 6 6 6 2: 7 7 3: 8 8 8 2: 9 9 3: 10 10 10 3: 11 11 11 2: 12 12 3: 13 13 13 3: 14 14 14 1: 15 ...

In reply to Re: reading from a socket, a little at a time by Anonyrnous Monk
in thread reading from a socket, a little at a time by Anonymous Monk

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.