Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re^7: IO::Socket Listen

by Eliya (Vicar)
on Dec 06, 2011 at 15:03 UTC ( #942044=note: print w/replies, xml ) Need Help??

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

When you know the length of the expected response (such as 131), you can concat the pieces you get from sysread and check if the resulting string has reached that length.  I said "pieces" because in theory (for example, if the connection has temporarily stalled for some reason), sysread might return prematurely with less than the requested number of bytes. So, to be sure, you'd need some loop + concat:

my $len = 131; my $resp = ''; while ( sysread $socket, $_, $len ) { $repsp .= $_; last if length($resp) >= $len; }

Replies are listed 'Best First'.
Re^8: IO::Socket Listen
by Secalles (Initiate) on Dec 07, 2011 at 11:29 UTC

    Eliya , your code provide some improvements in the performance it dont take 5 minutes anymore it takes between 30sec and 2minutes , is there anyway to make this faster? Thanks!

    Here is my code now

    my $socket = new IO::Socket::INET ( PeerAddr => '', PeerPort => '6008', Proto => 'tcp', ); die "Couldn't connect: $!\n" unless $socket; $data = pack("CccccccCcCC",02,65,63,80,81,76,86,03,100,13,10); if ( $socket ) { print $socket $data ; my $len = 131; my $resp = ''; while ( sysread $socket, $_, $len ) { $resp .= $_; last if length($resp) == $len; } print "$resp\n"; } close($socket);
      Try the unbuffered read() function instead of sysread(). Do not mix buffered and unbuffered reads! That will cause big trouble!

      You should be able with wireshark to tell if the problem is in the receive buffering or in the transmit delay from the other end. I am suspecting however that there is some delay while processing these very short packets. Although the time frame seems to be incredibly long! Can you provide more timing info from wireshark? What happened when you did: $socket->flush(); after the write (in Perl lingo the "print"?

      Update: I was confused.. corrected and also another suggestion posted.

        Try the unbuffered read() function instead of sysread()

        You've got it the wrong way round.  Perl's read function is buffered, while sysread is unbuffered.   sysread is implemented via tha low-level system (OS) call read(2) (man 2 read), which is why it's called sysread.  It's this read(2) that the documentation of sysread is talking about.

        That said, flushing the socket after the print is certainly a good idea, though.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2018-05-23 14:53 GMT
Find Nodes?
    Voting Booth?