in reply to Socket hangs

your scripts hangs here:
while(<$addr_handle>) {
because it keeps trying to read until the EOF is reached, but this only happens if the remote server closes the connection.

To read (or write) from sockets without blocking you have to use sysread, syswrite and select. Any good book about Unix or Network programing explains how to do it (usually in C, but in Perl it's almost the same), Stein's "Network Programing with Perl" is also a good book. And I am sure there are also several tutorials online.

Replies are listed 'Best First'.
Re^2: Socket hangs
by navarro (Initiate) on Dec 23, 2005 at 14:36 UTC
    Thnks for pointing this out for me Salva, but i still have same problem:
    # ./client.pl Command> uname Linux
    Here is the code after sugested changes:
    sub listen() { $local = IO::Socket::INET->new(Listen=>1, Proto=>'tcp', LocalAddr=>'eth0', LocalPort=>5000, ReuseAddr=>1,) or die "$!"; $local->autoflush(1); $sel = new IO::Select($local); while(@ready = $sel->can_read) { print "Command> "; foreach $fh (@ready) { if($fh == $local) { # Create a new socket $new = $local->accept; $sel->add($new); $comm = <STDIN>; print $new $comm; sub closeback() {exit(0)} while(sysread($new,$buffer,1024)) { print $buffer } } } } }
    It`s still freezing, what is wrong? Thank you!