To read in "binary mode" (that is, to read data that isn't structured into lines), you have to figure out when to stop reading one thing and start reading another. The simpest case is when you want to read a fixed number of bytes, in which case you can just use read. Some protocols will send a length byte followed by some data, in which case you'll read the length byte, then read the rest. Some protocols will keep sending data until a delimiter, in which case you can read one byte at a time until you find the ending delimiter (or process it as text, setting the $/ variable). As you can see, exactly how to do it depends on exactly how the data is formatted.

To write in binary mode, use print.

pack and unpack can also be useful for parsing some types of binary data.

I believe you'll also want to use binmode($sock) to put the socket in binary mode, which will avoid automatic translation between DOS/Windows and Unix style line endings.


In reply to Re: read binary datas by sgifford
in thread read binary datas by doctor_moron

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.