in reply to Getting stuck reading from a socket

The script is getting stuck at '$resp = <$sock>;'

Yeah, that's a common problem with sockets. You usually use IO::Select to read sockets reliably, but if you want to go simple, try either of these:

my $msg; $sock->recv($msg, 1024); # or sysread ( $sock, my $line, 1024);

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: Getting stuck reading from a socket
by punungwe (Initiate) on Jul 12, 2012 at 08:36 UTC

    Thank you very much

    This works

Re^2: Getting stuck reading from a socket
by punungwe (Initiate) on Jul 12, 2012 at 11:39 UTC
    Oops! I spoke too soon. It doesn't work if the hardware does not respond.
      If the hardware dosn't respond, there is no program in the world that will read anything out of that socket. You need to explain/explore the communication protocol of the hardware device, and find out what it needs to trigger a response. All I can do is make guesses in the dark.

      It might be as simple as adding a newline to your send:

      $sock->send( "$msg\n" ); # possibly " $msg\r\n " # or send a blank line $sock->send( "\n" );
      to tell the hardware you are done sending your line.

      You might also try something like this hack to inefficiently read 1 byte at a time

      while(my $len = sysread( $sock, my $buffer, 1) > 0) { print "$buffer\ +n"; }
      Otherwise, you need to provide much more information about your hardware. Does the hardware have a driver written in c or c++? Can you look at the driver's source code to see what it does? Does the hardware have any driver that works?

      Finally, when you use the socket methods send and recv, you are establishing a 1 way-at-a-time protocol. The hardware device might be getting locked into recv mode.

      You might be better off using IO::Select on the socket and using syswrite and sysread instead of send and recv.

      A simple IO::Select program might look like this:

      #!/usr/bin/perl use warnings; use strict; my $sock = new IO::Socket::INET( PeerAddr => $host, PeerPort => $port, Proto => 'tcp', Reuse => 1 ); $sock->autoflush(1); # latest Sockets has this on by default print $sock "log in information"; my $read_set = new IO::Select($sock); my $incoming_data = ""; while (1) { my @ready = $read_set->can_read(.5); foreach my $rh (@ready) { sysread ( $rh, my $line, 1024); $incoming_data .= $line; print "$incoming_data\n"; } }

      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh