• 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 => '',
                                  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) {
  #Retrieve only header
  my ($taskid, $size, $clientId, $status, $command) = unpack "C4 C4 C4 C4 C4", $buffer;
  if ($size >= $request_size)
     my ($channelNum, $refresh, $ctAddress, $counter1, $counter2) = unpack "C4 C4 C4 C8 C8", $buffer;
     print "channel num $channelNum\n";
     print "ERROR\n";
  #Parse data -  
  #Payload - each non-counter field 4 bytes = total length - 4*4+8*numCounter)


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

Did you mean unpack "A4 A4 A4 A4 A4" ?
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?

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!

> The header consists of 24 bytes, 4 bytes per field.
That sounds like
unpack "a4"x6,$buffer;
unpack "A4"x6,$buffer;
depending on what you'd want to do with any space or null padding
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.  
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
to check which of multiple servers has a socket ready for input or output

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