?
Solved

IO::Socket::INET read one byte at a time

Posted on 2006-06-29
6
Medium Priority
?
8,138 Views
Last Modified: 2009-12-16

I am writing a client that will connect over the network to a server. The server will send (upon connect) multiple bytes of binary data, I need the client to log all these bytes of data send by the server. I would also want to handle the input one byte at a time if possible.

I have written a client as below, however, the client doesn't seems to do blocking correctly, it doesn't display anything until the server timeout and disconnect. The question also is how I can get the input one byte at a time. I've tried "$remote->recv($data_read, 1)" but that doesn't seems to work.....

---------------------------------------------------------
#!/usr/bin/perl -w
use IO::Socket;
$remote = IO::Socket::INET->new(
     Proto    => "tcp",
     PeerAddr => "server",
     PeerPort => "1234",
) ;
unless ($remote) { die "cannot connect to remote" }
$remote->autoflush(1);

while ( <$remote> ) { print }

close $remote;
---------------------------------------------------------

Would really like to know how I can get this client to get the binary bytes from the server.

Thanks...
0
Comment
Question by:dillama
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
6 Comments
 
LVL 25

Expert Comment

by:clockwatcher
ID: 17015771
Give this a try:

#!/usr/bin/perl -w
use IO::Socket;
$remote = IO::Socket::INET->new(
     Proto    => "tcp",
     PeerAddr => "server",
     PeerPort => "1234",
) ;
unless ($remote) { die "cannot connect to remote" }
$remote->autoflush(1);

binmode $remote;

my $buffer;
my $numBytesToRead = 1;
my $bytesRead;
my $readcount = 0;

while ($bytesRead = read($remote, $buffer, $numBytesToRead))
{
   $readcount++;
   print $readcount;
   for (my $i = 0; $i < $bytesRead; $i++)
   {
       my $byte = substr($buffer,$i,1);
       print "\t".ord($byte);
       print "\t$byte" if ord($byte) > 26;
       print "\n";
   }
}
print "Read Error: $!\n" unless defined($bytesRead);

close $remote;
0
 

Author Comment

by:dillama
ID: 17025808
That worked great!

Actually, these binary inputs are actually integer type (4 bytes) (feed by a C program). How would I be able to feed it back into my perl program as variables? I know I can manually mess with the input 4 bytes at a time to convert it into a number value (integer), but are there any shortcut (or perl function) that allow me to do this?

Increasing the points to 250 to compensate for extra requirements.
0
 
LVL 25

Accepted Solution

by:
clockwatcher earned 1000 total points
ID: 17027702
Use the unpack function.. but to really answer it you need to know whether it's coming in big-ending or little-endian-- see the code below:

#!/usr/bin/perl -w
use IO::Socket;
$remote = IO::Socket::INET->new(
     Proto    => "tcp",
     PeerAddr => "server",
     PeerPort => "1234",
) ;
unless ($remote) { die "cannot connect to remote" }
$remote->autoflush(1);

binmode $remote;

my $buffer;
my $numBytesToRead = 4;
my $bytesRead;
my $readcount = 0;

while ($bytesRead = read($remote, $buffer, $numBytesToRead))
{
   $readcount++;

   if ($numBytesToRead < 4)
   {
      warn "Readcount: $readcount -- what's up with that... only read $numBytesToRead bytes";
   }

   @values = unpack("L*", $buffer);  # long machine dependent byte order-- little-endian on an intel
   # @values = unpack("N*", $buffer);  # big-endian long
   # @values = unpack("V*", $buffer);  # little-endian long
   
   print "$readcount\t$buffer\t";
   $f = 1;
   foreach $intvalue (@values)
   {
      printf "\t"x3 unless $f;
      printf "%x\t$intvalue\n", $intvalue;
      $f = 0;
   }
}
print "Read Error: $!\n" unless defined($bytesRead);

close $remote;
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 17027710
That warn should have been:

      warn "Readcount: $readcount -- what's up with that... only read $bytesRead bytes";

Personally don't know if it would ever happen.  And if it did it would only happen on a connection close.  The read will block 'til it gets $numBytesToRead bytes.  If the server kills the connection, you might get an undefined return from the read rather than the actual bytes it's read to that point.  Don't know.
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question