Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Reading Binary Data via socket ( Converting program from java to perl )

by petethered (Pilgrim)
on Oct 19, 2004 at 00:27 UTC ( [id://400357]=perlquestion: print w/replies, xml ) Need Help??

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

Ok, I have a chunk of javacode that I am trying to convert to perl and it's out of my realm of expertise. Javacode:
if(more) { byte b = 64; out.write(b); out.flush(); more = false; } int size = in.readInt(); imgData = new byte[size]; for(int i = 0; i < size; i++) imgData[i] = in.readByte();
I can follow what the Javacode is doing.

It sends a byte to the server, then reads in an int ( Since it's using readInt, im assuming the int gets passed back in plain text with a newline to end it ) then creates an array of bytes based on the size that was passed to it and then reads the bytes till the size is reached.

My problem: I don't know how to do this in perl. All of my experience with sockets is in plaintext ( mostly for webcommunication ) . Any chance of someone providing me a idea on how to do this?

PeteTheRed

  • Comment on Reading Binary Data via socket ( Converting program from java to perl )
  • Download Code

Replies are listed 'Best First'.
Re: Reading Binary Data via socket ( Converting program from java to perl )
by BrowserUk (Patriarch) on Oct 19, 2004 at 02:22 UTC
    Since it's using readInt, I'm assuming the int gets passed back in plain text with a newline to end it.

    No. ReadInt() reads binary data, not ASCIIfied. See ReadInt(). The (roughly) equivalent code in Perl would be:

    if( $more ) { syswrite( OUT, 1, chr( 64 ) ); $more = 0; } sysread( IN, 4, my $buf ); my $size = unpack( 'N', $buf ); ## Or 'V' see perlfunc:pack my $imgData; sysread( IN, $size, $imgData );

    That's only vaguely equivalent. Your snippet does not show what in, out or more are, so you'll need to work that out for yourself. It looks vaguely as if they are separate input and output handles to a socket stream or pipe, but that's guessing.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re: Reading Binary Data via socket ( Converting program from java to perl )
by BUU (Prior) on Oct 19, 2004 at 00:49 UTC
    You want pack and unpack. Pack to create the binary data, which you then print to the socket, then you use read to get data back and unpack to turn it in to perl usable scalars.
      Any chance of you expanding on your answer and giving me an example that will do what I want? ;)

      Yeah, I know, asking you to do the work for me.

Re: Reading Binary Data via socket ( Converting program from java to perl )
by pg (Canon) on Oct 19, 2004 at 02:12 UTC

    If you want to send binary data through a socket, you have to set it up with binmode:

    binmode($socketname);

    For a full exmaple, read A simple FTP server, see how it calls binmode when the data being FTP'd is image.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://400357]
Approved by kvale
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-25 20:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found