Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Problems? Is your data what you think it is?
 
PerlMonks  

byte by byte by byte...

by SirBones (Friar)
on Jun 10, 2004 at 16:03 UTC ( [id://363173]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

SirBones has asked for the wisdom of the Perl Monks concerning the following question:

Migrating some Java TCP/IP client code to Perl. The app communicates over a TCP socket; the messages consist of varying length binary buffers (not text strings). Since I'm a Perl newbie, I'm finding it difficult to map my c and Java thinking to Perl's handling of arrays and buffers. I'd appreciate some brief suggestions on the best way to implement these functions.

In Java, the data structure I used for the message buffer was a byte array. So, for example, I would manually set up the first 4 bytes of the message like this (our protocol expects the first 4 bytes to be an integer data length):

byte[] commandBuffer; int cmdBytes; . . . commandBuffer[0] = (byte) ((cmdBytes >> 24) & 0x000000ff); commandBuffer[1] = (byte) ((cmdBytes >> 16) & 0x000000ff); commandBuffer[2] = (byte) ((cmdBytes >> 8 ) & 0x000000ff); commandBuffer[3] = (byte) (cmdBytes & 0x000000ff);

Conversely, when the client receives a message, he (or she) needs to decipher the first 4 bytes to get the length of the incoming buffer.

The buffer itself obviously needs to be manipulated in various ways (binary data parsed and written.) In Java I had a series of methods which basically walked through the buffer byte by byte and stuffed, extracted, and processed as required. Can someone provide guidance as to the most efficient way to handle this in Perl? I take it from some of my initial reading that walking through a string byte by byte is usually not necessary or desirable. And I'm not sure I know how to do something like...

commandBuffer[47] = 0xfc;

...in the Perl idiom in any case.

Not looking for detailed recipes (I know there are a lot of questions here), but just some pointers and examples to get me going in making this transition.

Thanks in advance.

Replies are listed 'Best First'.
Re: byte by byte by byte...
by hardburn (Abbot) on Jun 10, 2004 at 16:09 UTC

    Your best bet is probably unpack. That contains a template to grab things byte-by-byte. You can then put it directly into an array.

    ----
    send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

Re: byte by byte by byte...
by Plankton (Vicar) on Jun 10, 2004 at 16:18 UTC
    Maybe reading up on pack/unpack and read would be helpful to you.

    Plankton: 1% Evil, 99% Hot Gas.
Re: byte by byte by byte...
by BrowserUk (Patriarch) on Jun 11, 2004 at 01:33 UTC

    You may find PerlPackTut a somewhat better introduction to pack & unpack than the bare perlfunc pod.

    As a starting place for your application, this might help. This packs five bytes, hex values 0x40 - 0x44 into a string, prefixes it with a 32-bit little-endian byte count and then unpacks the whole thing and prints the length followed by the byte values interspered with '|'s.

    print join'|', unpack 'V C*', pack 'V/A*', pack 'C*', 0x40, 0x41, 0x42 +, 0x43, 0x44; 5|64|65|66|67|68

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
      That looks great...just what I'm looking for. Thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://363173]
Approved by coreolyn
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.