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 $="

In reply to Socket Creation fails on Win32 by acid06

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.