Title: RE: [Perl-unix-users] sysread, sockets, reading binary data

Jason,
     what about something like.

my @bytearray;
push @bytearray, unpack "C*", $bufrcv;

foreach my $byte (@bytearray) {
        ...
}


Steve Aaron



-----Original Message-----
From: Jason Ostrom [mailto:[EMAIL PROTECTED]]
Sent: Monday, November 19, 2001 6:49 PM
To: [EMAIL PROTECTED]
Subject: [Perl-unix-users] sysread, sockets, reading binary data



Hello guys and girls,

I am writing a TCP client program that connects to a server, sends
binary data, and should receive a message back.  The problem I have am having
is on the receiving end.  I was wondering if any of you have
implemented something like this and may be able to help.

I want the
program to be able to read the socket and, byte by byte, insert data
from the TCP stream into data structure, such as an array, so that
each byte may be referenced later.

Here is the message construction:
################################################
$Len1 = 0x00;
$Len2 = 0x0A;
$SVCA = 0x96;
$DVCA = 0xDF;
$CTRL = 0x40;
$STAT = 0x00;
$FUNCID = 0x83;
$ACCESS = 0x04;
$RGB1 = 0x00;
$RGB2 = 0x00;
$RGB3 = 0x00;
$RGB4 = 0x0A;
#################################################

# Message construction
$bytestring = pack('C*', $Len1, $Len2, $SVCA, $DVCA, $CTRL, $STAT, $FUNCID, $ACCESS, $RGB1, $RGB2, $RGB3, $RGB4);

print ("Begin now to send message to socket after $waittime seconds\n");
sleep $waittime;
$len = length($bytestring);

#######
# Here is the routine that sends to the socket...Needs work
#######
$offset = 0;
$bytes_to_write = $len - $offset;
$bytes_written  = 0;
while ($bytes_to_write) {
        $bytes_written = syswrite ($socket, $bytestring, $bytes_to_write, $offset);
        $offset         += $bytes_written;
        $bytes_to_write -= $bytes_written;
        }

#######
# Receiving section
#################

$offset = 0;
$bytes_to_read = length(<$socket>);
while ($bytes_to_read) {
        $bytes_read = sysread ($socket, $bufrcv, $bytes_to_read, $offset);
        $bytes_to_read -= $bytes_read;
        $offset += $bytes_read;

}



Now I can see with TCPDUMP on this Red Hat 7.2 machine that the server
is sending back the message.  I just need to be able to read the
message into a data structure, a byte at a time.

Any help very much appreciated.
-jason

_______________________________________________
Perl-Unix-Users mailing list. To unsubscribe go to http://listserv.ActiveState.com/mailman/subscribe/perl-unix-users

Reply via email to