cspctec has asked for the wisdom of the Perl Monks concerning the following question:

I've built a script to connect to a device via a socket handle through the telnet port, but when I try to assign a variable to the socket handle to store information, my script hangs...
my $socket = IO::Socket::INET->new( PeerAddr => '192.168.1.2', PeerPort => '23', Proto => 'tcp' Timeout => 5) or die "Connection failed!"; my $results = <$socket>;

If I assign the results variable to my socket handle, the script will just hang and not output anything.

What would be the most likely reason for it hanging when assign the handle to a variable? Do you think it could just be network lag at on my company intranet? Assigning the socket handle to a variable works fine with other identical devices on the network. I can't figure out why it won't work with this one specific device.

Replies are listed 'Best First'.
Re: Script hangs when assigning variable to socket handle
by aitap (Curate) on Mar 05, 2013 at 19:01 UTC

    The <> operation (thus, readline($socket)) waits for the contents of $/ variable to appear on the socket before returning. Are you sure that remote host sends data containing it after someone connects to it?

    Try setting your socket into non-blocking mode by adding Blocking => 0 to the constructor arguments. More useful information on non-blocking I/O can be found at http://www.kegel.com/dkftpbench/nonblocking.html and in perlfaq8.

    Sorry if my advice was wrong.
Re: Script hangs when assigning variable to socket handle
by kielstirling (Scribe) on Mar 05, 2013 at 20:25 UTC
    Hangs? Or waits on a read? you may find strace (truss id Solaris) useful. Google it.