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

Wise monks,

The following simple code crashes on win32:

use IO::Socket::INET; IO::Socket::INET-> new( Listen => 5, LocalPort => 10000, Blocking => 0, ReuseAddr => 1, ) or die "error:$!";
I can see that it crashes because of blocking=0, and I can see that IO::Socket::INET itself is possibly not to blame, because the same code works fine on unix. I can also see that even IO.xs is ok, because it calls fnctl() which, seems to me, is present on win32. But I don't see how come that this doesn't work? Is that a bug? a feature? Please give me clues as I have none.

Replies are listed 'Best First'.
Re: IO::Socket::INET on win32
by BrowserUk (Patriarch) on Dec 12, 2007 at 06:24 UTC

    Which version of Perl? Cos I can't get it to crash on any of the versions I have available.

    It doesn't succeed, but that's not the same as "crashing". And if you print out the extended error information you get a clue as to why:

    use IO::Socket::INET; my $s = IO::Socket::INET-> new( Listen => 5, LocalPort => 10000, Blocking => 0, ReuseAddr => 1, ) or die "error:$! [$^E]"; print $s; __END__ c:\test>junk error: [Incorrect function] at c:\test\junk.pl line 2.

    The reason for the failure is that the Blocking => 0 parameter is nt supported on win32. Is is possible to set sockets non-blocking on win32, but you have to use an ioctl call to do it.

    A supersearch for "0x8004667e" turns up a whole bunch of previous discussion and examples.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Yes, it's not "crashing" but exiting with an error, sorry. So if I'd publish portable code, what are best practices? Pepper the code with ioctl() if $^O =~ /win32/i or don't use IO::Socket::INET altogether, or use something else? I've seen POE calls ioctl() for versions less than 5.8, but calls $fh->blocking(0) otherwise... So that's incorrect, still use ioctl?

      I've tried on 5.8 and 5.10, same thing.

        Pepper the code with ioctl() if $^O =~ /win32/i ...

        Well, at the application level, probably the best thing you could do is override the IO::Socket constructor, strip the Blocking parameter on before instantiating the socket, and apply the ioctl() on the way out. Unfortunately, I don't think that will get you very far as I don't think the setting would be applied to sockets return by accept(), and then it starts getting messy.

        Of course, the best thing would be to apply/submit a patch to IO::Socket to enable the correct behaviour for Win32, but given the length of time that the 'fix' has been known, the fact that none of the experts hereabouts and elsewhere have applied such a patch could suggest that it is non-trivial.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.