• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

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);
0
jewee
Asked:
jewee
1 Solution
 
ozoCommented:
Could the server be closing the conection after sending only 96 bytes of data?

What happens if your loop is
while( 1 ){
0
 
manav_mathurCommented:
>Could the server be closing the conection after sending only 96 bytes of data?
"while($socket->connected)" should handle this, no??

I think whats happening is
- you send a request across (outside of the loop)
- the server serves it by sending across the header and the corresponding data.
- This you read in the first iteration of your while and print on terminal/captureFile

At this point of time, the connection is there. The server sits waiting for a request from the client (your program). The client sits waiting expecting the server to send another set of header followed by data.

Ideally,
1) your "while($socket->connected)" should be replaced with "if($socket->connected)"
or
2) the
$requestMsg = pack 'C*',@requestBytes;
  $socket->write($requestMsg);

should be inside the while loop. In that case, you should also be codng a break from the loop, otherwise, your client will poll the server continuosly.

Manav
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now