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

Hi, I have a Problem with blocking INET reading. I created a socket and wait till a client connect to it. But no matter what I do, whenever I try to read from the socket, it blocks. I tried $Client->blocking(0), but it seems to have no effect. I tested it under Win2000 with Perl 5.6.1. Does anyone kown how I can solve this problem? Code:
$Socket = IO::Socket::INET->new( LocalPort => '25022', Proto => 'tcp', Listen => 10, Timeout => 1, Type => SOCK_STREAM, Reuse => 1 ) or die $!; until($Client = $Socket->accept()) {} $Client->blocking(0); # The next statment shouldnt block, but it does !!! $Input = <$Client>; $Client->blocking(1);
Later in the program I need the blocking. Has this anything to do with Perl or Windows Version, I have problems with sockets under Win NT 4.0 too, but on the Client side ( but this is another story ). Andre

Replies are listed 'Best First'.
Re: Non-Blocking INET getline
by matija (Priest) on Nov 22, 2005 at 13:22 UTC
    Reading with <> will block, because the string it returns is guaranteed to be terminated with a newline, (or the last non-terminated string before the end of communication). I agree that you should use sysread when reading from sockets.
Re: Non-Blocking INET getline
by monarch (Priest) on Nov 22, 2005 at 12:54 UTC
    This is only a suggestion.. but try using sysread() instead of <$Client>. Also you can specify Blocking => 0 as an argument to the socket object creation method.
Re: Non-Blocking INET getline
by Outaspace (Scribe) on Nov 23, 2005 at 11:51 UTC
    I tried:
    sysread($Client,$szInput,1)
    Sorry but sysread blocks too. Is there no way to check the socket first, before I try to read somrthing from it ???