I have a socket program that I believe is suffering from input buffering (not output buffering).

I can post example code if you want, but it's easy to describe. I connect/accept a socket between two processes and set the socket to not use buffering ($|=1).

One side writes several messages in a row, lets call them mesgA, mesgB, mesgC, sleeps a while then sends mesgD. The other side is in a loop that uses select() to look for incoming messages and processes 1 message each time select says a message is available.

Eg:

$rin = ''; vec($rin,fileno($sd),1) = 1; $nfound = select($rin, undef, undef, 1.0); if ($nfound) { if ($line = <$sd>) { ... } else { ... knows other side closed the socket ... } }
The above code always sees mesgA, but doesn't see mesgB etc until the other side sends mesgD when select() finally realizes more data is on the socket.

The problem isn't the usual output-buffering problem. I know this because if I modify the receiver to ignore what select() says and simply read from $sd, mesgB and mesgC are always there right away.

I believe the problem is input buffering. select() is probably looking quite literally only at the socket, and mesgB/C probably aren't on the socket anymore, they've probably been buffered by the receiver.

So now finally to the questions: how can I deal with that? I need to either 1. turn off the input buffering so mesgB/C will still be on the socket where select() will see them so I can be notified of their existance, or 2. have something like select() that lets me know more data is available to read (even if it's not technically on the socket).

Basically how am I supposed to know how much data is sitting there for me to read? I need to read however much is there (without blocking).

Really I think it should be select()'s job to look at the socket and the input buffer and return true for that socket if data is available.. that's the point of select.. to ask "will a read block or not".


In reply to select() and input buffering by declan

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.