Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re^2: IO::Socket Listen

by Secalles (Initiate)
on Dec 05, 2011 at 16:37 UTC ( #941893=note: print w/replies, xml ) Need Help??

in reply to Re: IO::Socket Listen
in thread IO::Socket Listen

Thanks i did this , yeah i did it the first time but i got no response , so i tryed 2 sockets ( thinking that the device could responde faster than i could listen ) , but this program takes 5 minutes to respond , what can i do to make it faster?

Replies are listed 'Best First'.
Re^3: IO::Socket Listen
by Eliya (Vicar) on Dec 05, 2011 at 20:11 UTC
    but this program takes 5 minutes to respond

    The problem probably is that the device isn't sending a newline, so the <$sock> (i.e. readline) keeps waiting for it until the socket is finally closed — which is likely what is happening due to a timeout after those 5 minutes...

    Try sysread instead (with a length value larger than the response you expect).

    while ( sysread $sock, $_, 1000 ) { print $_; }
Re^3: IO::Socket Listen
by Marshall (Abbot) on Dec 06, 2011 at 10:24 UTC
    I think you need to read the spec on the I/F to this server. You are sending <CR><LF> at the end of your packet, but many of these things work with fixed length packets instead of line oriented packets.

    It could be that you need to send perhaps 128 bytes to it instead of a buffer ending in CRLF? Note: that CRLF is the network line ending, not just <LF>, so that part is right as far as it goes...However it could be that this thing is waiting for more bytes from you... Maybe..Send minimum 128 bytes? Read their spec..

    Usually the smallest packet that will be sent is 128 bytes. Sometimes a 256 byte message will take a couple of packets even on the local machine.

    Try just printing whatever the server returns to you on the first sysread() attempt. That will give you a clue as to what it is doing. If all you are expecting is less than 128 bytes, then you are probably done!

    FYI, A Perl client read routine to get a 512 byte fixed packet might look like this:
    my $buf = readn ($socket, 512);

    sub readn { my ($socket, $bytes ) = @_; my $offset = 0; my $buf = ""; while ($offset < $bytes) { my $nread; my $nleft = $bytes-$offset; $nread = sysread($socket,$buf,$nleft,$offset); kill 'USR1',$$ unless (defined $nread); ## undef is like -1 uni +x return last if ($nread ==0); ## EOF $offset += $nread; } # print "length of received buff=",length $buf,"\n"; # print $buf; return $buf; }
    Update: Perl will work with line oriented packets GREAT! But if that is not what this thing is sending back, then this is for naught. print the results of the first sysread() and see where that leads.
    ***Also perhaps try a flush() on your end! Normal socket IO is buffered. $socket->flush().

      I have already read the spec , the device is a High Power Amplificator and i'm sending commands to another equipment that controls the HPA called controller,acording to the spec i have to send CR and LF so the controller can understand what i want ( in this case i want the output power level ) to be honest the specs don't have anything useful about the communication.

      The lenght of the pakage that i receive is 131 bytes ( it sends 2 pakages one with 63 bytes and another with 68 bytes i'm using wireshark to trace the pakages)

      i used Eliya solution and I still have a big delay

      Here is the answer of my program

      &#9824;A?42.3< 30D 0.111.3&#9829;) 210 Seconds Delay
      Changed 1000 to 131 and 2000 but still no fast response
      while ( sysread $socket, $_, 131 ) { # 2000 , 1000 print $_; }
         &#9824;A?42.3< 30D 0.111.3&#9829;) 210 Seconds Delay

        Do you mean you're actually getting (i.e. reading/printing) the values on the client side, and then there is the delay?

        If so, you probably want to close the socket (or exit the loop) after having gotten the (complete) response.  Otherwise, the while loop with the sysread will continue to repeat until the remote side itself closes the socket (i.e. sysread sees EOF and returns 0).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://941893]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2018-06-21 05:45 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.