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 20:09 UTC | |
|
Re: byte by byte by byte...
by BrowserUk (Patriarch) on Jun 11, 2004 at 05:33 UTC | |
by SirBones (Friar) on Jun 11, 2004 at 12:45 UTC | |
|
Re: byte by byte by byte...
by Plankton (Vicar) on Jun 10, 2004 at 20:18 UTC |