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
Solved

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

Posted on 2006-06-29
6
8,104 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
  • 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 250 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl script to parse log and delete the file 17 176
Perl strange behaviour 5 73
perl CPAN issue 3 116
Bash one liner, start and end time calculation 13 115
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

790 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