Link to home
Start Free TrialLog in
Avatar of jewee
jewee

asked on

Why does socket program only collect once! - Perl!!

Why does this only do one read?   I have a socket connected to a server.  It sends a request, then does 2 reads - one reads the header while the other reads the rest of the data.  After one run through the while loop, it hangs!




#! /usr/bin/perl

open captureFile, ">capture_file.bin";

use IO::Socket;

my $socket = new IO::Socket::INET (
                                  PeerAddr => '10.0.0.5',
                                  PeerPort => '2222',
                                  Proto => 'tcp',
                                 );
die "Could not create socket: $!\n" unless $socket;



@requestBytes=( 0xD0, 0x00, 0x00, 0x00,
                0x28, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x65, 0x00, 0x00, 0x00,  
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x07, 0x00, 0x00, 0x00 );
               
#print "@requestBytes\n";

$requestMsg = pack 'C*',@requestBytes;
  $socket->write($requestMsg);


while($socket->connected) {
  $buffer=0;
  #Retrieve only header
  $socket->read($buffer,40);
 
  my ($taskid, $size, $clientId, $status, $command, $driverError, $channelNum, $refresh, $ctAddress, $numOfCounters) = unpack "V10", $buffer;
  print "t: $taskid, s: $size, c: $clientId, st: $status, cm: $command, de: $driverError\n";
 
         print "Num of counters $numOfCounters\n";

      print "channel num $channelNum\n";  
        print "ct address $ctAddress\n";
 
  print "size $size\n";
 $socket->read($buffer1,56);
     
print captureFile $buffer1;

printf( "%32x\n", unpack( "H112", $buffer1) );

my ($counter1, $counter2, $counter3, $counter4, $counter5, $counter6, $counter7) = unpack "Q7", $buffer1;
 
printf "c1: 0x%x, c2: 0x%x, c3: 0x%x\n", $counter1, $counter2, $counter3;

 
       
     
  sleep(1);
}

close($socket);
Avatar of ozo
ozo
Flag of United States of America image

Could the server be closing the conection after sending only 96 bytes of data?

What happens if your loop is
while( 1 ){
ASKER CERTIFIED SOLUTION
Avatar of manav_mathur
manav_mathur

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial