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

I'm writing an application which needs a web interface, so I found the module Net::HTTPServer, which seemed to do its job nicely. However, when I first ran the example snippet from the module's documentation, it failed. I investigated it and found out that the module was failing when trying to create the socket:
$self->{SOCK} = new IO::Socket::INET(LocalPort=>$port, Proto=>"tcp", Listen=>10, Reuse=>1, Blocking=>0);
Neither $! nor Win32::GetLastErorr could tell what happened. So I managed to find out that commenting the Blocking=>0, the socket would be created successfully. However, after doing this, it would fail in another subroutine:
###################################################################### +######### # # _nonblock - given a socket, make it non-blocking # ###################################################################### +######### sub _nonblock { my $self = shift; my $socket = shift; #----------------------------------------------------------------- +--------- # Code copied from POE::Wheel::SocketFactory... # Win32 does things one way... #----------------------------------------------------------------- +--------- if ($^O eq "MSWin32") { ioctl( $socket, 0x80000000 | (4 << 16) | (ord('f') << 8) | 126 +, 1) || croak("Can't make socket nonblocking (win32): $!"); return; } #----------------------------------------------------------------- +--------- # And UNIX does them another #----------------------------------------------------------------- +--------- my $flags = fcntl($socket, F_GETFL, 0) || croak("Can't get flags for socket: $!\n"); fcntl($socket, F_SETFL, $flags | O_NONBLOCK) || croak("Can't make socket nonblocking: $!\n"); }
Even if I tried setting $self->{SOCK}->blocking(0), it would still fail inside the _nonblock subroutine. Eventually, I tried transforming _nonblock() into a dummy subroutine, which just returns, and the example ran just fine: I could, finally, access the webserver from my browser. However, I wanted to know how do I make the sockets non-blocking on Win32 if I ever need to do it. Or if that's the right approach, why was it failing. Thanks in advance.


acid06
perl -e "print pack('h*', 16369646), scalar reverse $="

Replies are listed 'Best First'.
Re: Socket Creation fails on Win32
by traveler (Parson) on Feb 22, 2004 at 22:33 UTC
    This node seems to have a variety of answers to your question.