Problem with socket!

Posted on 2005-04-05
Last Modified: 2010-05-18
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)


Question by:jewee
    LVL 84

    Expert Comment

    It works for me, are you sure the server is sending anything?
    Are you sure you want unpack "C4 C4 C4 C4 C4"?
    LVL 84

    Expert Comment

    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" ?

    Author Comment

    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?

    LVL 84

    Accepted Solution

    > 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

    Author Comment

    But it sounds like i need to use asynchronous sockets, however, I have only heard about asynch sockets in C++, not in perl.  
    LVL 84

    Expert Comment

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
    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…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now