http://www.perlmonks.org?node_id=89382

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

Hi, I hope that this is a really silly newbie question, revolving around a simple mistake. (: I'm trying to communicate with a data server, providing stock quotes and the like. I'm quite new to perl, and very new to socket programming, so I'm not entirely sure what I'm doing... I've been reading through the search results for a couple days, but I'm still stumped. The header of each packet is in binary, followed by a bunch of ASCII data, in the format: 1 byte ascii sync byte (character 002) 2 byte message size (including header) 1 byte data offset (header size) 1 byte message type (To logon, I'm to send hex value 0x4c) 4 byte sequence id number 2 byte protocol id (always 0) 2 byte version number (1) And then the ASCII data consisting of login and password information. I'm not sure if y'all will be able to help with this at all -- to many variables, I'm too lost... It might not even be a perl problem -- but I thought that if I posted the code someone might have some good advice. It complains that the message length is invalid -- am I not communicating the size correctly? Or am I doing something silly that makes the header much bigger than it should be? Thank you, and I'm sorry this is so involved...
#!/usr/bin/perl -w use IO::Socket; $rsock = new IO::Socket::INET (PeerAddr => 'blah', PeerPort=> 1234, Proto => 'tcp', ); die "Remote socket could not be created: $!\n" unless $rsock; $str[0]="\002"; $str[1] = unpack("B*", pack("N", 47)); $str[2] = unpack("B*", pack("N", 12)); $str[3] = unpack("B*", pack("N",76)); $str[4] = unpack("B*", pack("N", 1)); $str[5] = unpack("B*", pack("N", 0)); $str[6] = unpack("B*", pack("N", 1)); $header=join('',@str); $header=$header."LOGON USERNAME=NAME PASSWORD=PASS\0"; print $rsock $header; #syswrite($rsock,$header,47); # Print, or syswrite? Neither seem to help... while (<$rsock>) { last unless /\S/; chomp; print; } # Returns Error: Ileagle message size in header

Replies are listed 'Best First'.
Re: Socket Newbie (perl newbie, too...)
by btrott (Parson) on Jun 18, 2001 at 22:29 UTC
    I think you're misunderstanding the packet format. When you say that the representation is in "binary", it doesn't mean the binary representation that you get by doing unpack "B*"; it means that your data is packed as if it's in a binary structure. So you have the right operator (pack), you just have the wrong pack template.

    I think that this will work for you:

    my $data = "LOGON USERNAME=NAME PASSWORD=PASS\0"; my $packet = pack "CnCCNnn", 2, ## sync byte length($data)+13, ## message size 13, ## header size hex("4c"), ## message type 1, ## sequence ID 0, ## protocol ID 1; ## version number $packet .= $data;
    That pack template ("CnCCNnn") says to pack the list of values (the rest of the arguments to pack) in the following way:
    • unsigned char value (1 byte)
    • short in network order (2 bytes)
    • unsigned char value (1 byte)
    • unsigned char value (1 byte)
    • long in network order (4 bytes)
    • short in network order (2 bytes)
    • short in network order (2 bytes)
    This varies from your use of pack in two important ways. First, we no longer use 'unpack "B*"', because the point of that is to take a binary structure and make it into a human-readable string that looks like a sequence of bits. That's not what you want: you want to send the data to the server as a binary structure.

    Second, you can't just use 'pack "N"'; that packs everything as a network-order long, which is 4 bytes. You don't want everything to be 4 bytes; some things are 1 byte, some are 2, some are 4. You have to vary you pack template depending on that.

Re: Socket Newbie (perl newbie, too...)
by damian1301 (Curate) on Jun 18, 2001 at 22:31 UTC
    Just as a sidenote-- if you want stock quotes and all that finance goodness then go check out Finance::Quote instead of all that socket stuff.

    $_.=($=+(6<<1));print(chr(my$a=$_));$^H=$_+$_;$_=$^H; print chr($_-39); # Easy but its ok.
Re: Socket Newbie (perl newbie, too...)
by wog (Curate) on Jun 18, 2001 at 22:20 UTC
    Your problem is in the use of unpack("B*", ...). The unpack there takes raw binary data and turn it into a human-readable string of 1s and 0s. The pack you are using already returns a raw data that is probably suitable for sending over the socket. (agh! see update below.)

    You should probably be using syswrite or you should turn off buffering.

    update: Oops, mental error, the pack format's wrong. See node below.

Re: Socket Newbie (perl newbie, too...)
by Anonymous Monk on Jun 18, 2001 at 23:19 UTC
    If you want to talk to a database server, you should check DBI. Or do you want to write a new driver module? I think it would be a hard task for a Newbie.