Hi all

I am trying to write a TCP client. The server sends responses in a proprietary format: 8-byte header, with op-codes in bytes 0-3, length in bytes of data that follow encoded in bytes 4-7, the the stated number of bytes which may be binary or ascii or a mix of both, and terminated with a linefeed.

For example, the response OFST -0.75
is encoded like this - notice the byte 7 with value 0xc == 11, for 10+1 character in the response:
810100000000000c4f465354202d302e3037350a
I started small and simple, from a code example similar to Camel 3 chapter 16 and got it basically working.

However, when the response data count happens to be 10, the byte 7 in above stream becomes 0xa.

IO::Socket::INET interprets it as end of input and returns the message truncated to the 8 header bytes:
VDIV 0.05 810100000000000
Question: can I configure IO:Socket::INET to receive data as binary and not stop receiving when it sees 0xa in the stream? How?

I did RTFM, but I failed to find the answer so far.

This is on Win2k, Perl 5.6 build 626.

Rudif
#!perl -w use strict; use IO::Socket; my $verbose = 1; print "===\n"; my $host = "127.0.0.1"; # or whatever my $port = 1861; my $remote = IO::Socket::INET->new( Proto => "tcp", PeerAddr => $host, PeerPort => $port) or die "Couldn't connect to $host:$port\n$!\n$@\n"; my $msg = 'ofst?'; # send query $remote->send(wrap($msg)) or die "Couldn't send: $!\n$@\n"; # read the remote answer my $answer = <$remote> || '---'; print "answer: =$answer=\n"; my $response = unwrap($answer); print "response: =$response=\n"; close $remote; exit; sub wrap { my ($data) = @_; my $header = pack "C4N", hex '0x81', hex '0x1', 0, 0, length($data +); return $header . $data; } sub unwrap { my ($packet) = @_; my ($op, $hvers, $count, $data); ($op, $hvers, undef, undef, $count, $data) = unpack "C4NA*", $pack +et; { my $warn = ''; my $length = length($packet); if ($count != $length) { $warn .= "warn: $count != $length: \n"; } $warn .= unpack "H*", $packet; print STDERR "$warn\n" if $verbose; } return $data; } __END__

Edited by footpad, 23 June 2001 - 20:46 (PDT)


In reply to IO:Socket::INET - how to receive binary data by Rudif

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.