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

they (protocols) can exchange datas in binary format and plain text, is that true ?, how perl read datas in binary format ?
example (in my dos 98 mode):

use strict; use IO::Socket; my $host= 'scsa.msg.yahoo.com'; my $port=5050; my $sock=new IO::Socket::INET(PeerAddr => $host, PeerPort=> $port, Proto=>'tcp'); die "cant open socket" unless ($sock); my $cmd="MYGET\n"; while(<$sock>) { print } close $sock;
when i ran this script i cant read any outputs....
any clues?

#iM not so goot in english :]

Replies are listed 'Best First'.
Re: read binary datas
by sgifford (Prior) on Sep 20, 2004 at 21:58 UTC

    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.

Re: read binary datas
by Plankton (Vicar) on Sep 20, 2004 at 21:43 UTC
    You might find this node (Bi directional Socket question) helpful. Also try doing a super search on ... maybe ... IO::Socket or network programming.

    Plankton: 1% Evil, 99% Hot Gas.
Re: read binary datas
by Errto (Vicar) on Sep 21, 2004 at 03:27 UTC

    I would guess (not knowing your protocol) that between the line my $cmd="MYGET\n"; and the line  while(<$sock>) { print } you probably meant to say print $sock $cmd;. Otherwise your $cmd isn't doing you any good.

    Also, please observe the Writeup Formatting Tips when posting to PerlMonks, including the use of <code> tags.