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

Problem with socket code...help!

500 points for anyone who helps me with this!  I am connecting to a server.  I was told to use asynchronous sockets when creating this api.


Here is my code, it is actually not receiving anything from the server.

use IO::Socket;

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


   #GHeader (24B):
      #TaskID = 0xD0 - constant
      #MsgSize = 40 - constant
      #Client ID = 0;
      #Status = 0;
      #Command - Get number of stats counters = 0x65
      #Reserved = 0;
      
   #GCPayload (16B):
         #ChannelNum = 0
         #Refresh = 0 (no refresh)
         #1st Count Address = 0 ???
         #Num of counters -
         
@requestBytes=(0xD0, 0x28, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x00, 0x01);

$requestMsg = pack 'C*',@requestBytes;

while($socket->connected) {
  $socket->write($requestMsg);
 
  #Retrieve only header
  $socket->read($buffer,24);
 
  my ($taskid, $size, $clientId, $status, $command) = unpack "C4 C4 C4 C4 C4", $buffer;
 
  if ($size >= $request_size)
  {
 
      $socket->read($buffer,$size);
 
     my ($channelNum, $refresh, $ctAddress, $counter1, $counter2) = unpack "C4 C4 C4 C8 C8", $buffer;
     print "channel num $channelNum\n";
  }
  else
  {
     print "ERROR\n";
  }
 
  #Parse data -  
  #Header:
  #Payload - each non-counter field 4 bytes = total length - 4*4+8*numCounter)

  sleep(1);
}





0
jewee
Asked:
jewee
  • 4
  • 2
1 Solution
 
ozoCommented:
It works for me, are you sure the server is sending anything?
Are you sure you want unpack "C4 C4 C4 C4 C4"?
0
 
ozoCommented:
unpack "C4 C4 C4 C4 C4"
is the same as
unpack "C20"
or
unpack "CCCCCCCCCCCCCCCCCCCC";
which returns 20 fields of which you only assign the first five.

Did you mean unpack "A4 A4 A4 A4 A4" ?
0
 
jeweeAuthor Commented:
The header consists of 24 bytes, 4 bytes per field.  

However, i believe I may want to implement asynchronous sockets since there are other processes connected to this as well?

0
Technology Partners: 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!

 
ozoCommented:
> The header consists of 24 bytes, 4 bytes per field.
That sounds like
unpack "a4"x6,$buffer;
or
unpack "A4"x6,$buffer;
depending on what you'd want to do with any space or null padding
 
0
 
jeweeAuthor Commented:
But it sounds like i need to use asynchronous sockets, however, I have only heard about asynch sockets in C++, not in perl.  
0
 
ozoCommented:
As described in
perldoc perlipc
Perl can handle asynchronous sockets in the server as C++ does by forking a slave server to handle each client request.
The client could also fork multiple processes if it wants to deal with multiple servers.
It could also use
select RBITS,WBITS,EBITS,TIMEOUT;
to check which of multiple servers has a socket ready for input or output
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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