Hi Sagacious Ones

Using IO::Socket::INET in a server context, I've been playing with how to read from a connected client. The client sends binary data of different 'types' (type 1 is one format that is variable in length, type 2 another, but predictable in length). There's an indicator near the beginning of the record that gives an idea of the record type followed by a couple of bytes telling how big the overall length is and a flag.

I thought I could get away with reading the first bytes of the header to determine the type, and then read some more afterward, like so:

   ## Byte 1 - type
   ## Byte 2 - y, flag
   ## Byte 3 - x, 8 + 12 * byte 4
   ## Byte 4 - entry count
   while ( <$client> ) {
        $client->read($raw_header, 6);
        $junk = unpack('H2', $raw_header);
        if ($junk eq "01") {
            print "Variable length is coming!\n";
            $more = unpack('H6', $raw_header);
            print "Three byte header is: $more\n";
            ## Flag byte, noted but not used
            $y = substr( $more, 2, 2 );
            ## Length of data calculated as 8 bytes + 12 * $ec
            $x = substr( $more, 4, 2 );
            print "X, the variable record length is: $x\n";
            print "Y, the flag is: $y\n";
            
            ## Read the entry count byte
            $client->read($raw_header, 2);
            $ec = unpack('H2', $raw_header);
            print "Sub-record Entry Count is: $ec\n";
        }

I was under the impression that I could do a second read() to get the following entry count byte so I could calculate the length and read() the rest of the data correctly... but I know from tcpdump output on the wire that the number $ec gets isn't the one I'm seeing from tcpdump

(A previous version of the code above had me pulling just one more byte and confirmed I was getting the right number for the entry count, but the question about this code segment above stands.)

I was under the impression I could read some bytes, then do something and read some more from where I left off, etc. Looks like I can't, so how would this be properly done?

Thanks,

In reply to 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.