Non-blocking socket connect examples on win32 not working

Discussion in 'Perl Misc' started by Ed W, Mar 18, 2005.

  1. Ed W

    Ed W Guest

    OK, yes I know this is practically an FAQ, but I can't get the win32
    non-blocking socket examples to work on Perl 5.8 on Win XP.

    See below for my example program. Seems to me that this should show a
    connect time of near zero seconds. However, it actually takes a time
    roughly the same as the ping time to the server, indicating to me that
    it's blocking until connected...

    Can someone help either pointing out the prob with my code, or at least
    validate that it's not just my machine!! I have tested with 5.8.0 and
    5.8.6 and see the same issue with both

    Thanks

    Ed W

    >>>>>>>


    use strict;
    use warnings;

    use Time::HiRes qw( gettimeofday tv_interval );
    use IO::Socket;

    BEGIN {
    if ($^O eq 'MSWin32') {
    eval '*EINPROGRESS = sub { 10036 };';
    eval '*EWOULDBLOCK = sub { 10035 };';
    eval '*F_GETFL = sub { 0 };';
    eval '*F_SETFL = sub { 0 };';
    *IO::Socket::blocking = sub {
    my ($self, $blocking) = @_;
    my $nonblocking = $blocking ? 0 : 1;
    ioctl($self, 0x8004667e, $nonblocking);
    };
    } else {
    require Errno;
    import Errno qw(EWOULDBLOCK EINPROGRESS);
    }
    }


    my $remote;

    if (! (
    $remote = IO::Socket::INET->new(
    Proto => "tcp",
    Type => SOCK_STREAM) ) )
    {
    print STDERR "Error creating socket: $@";
    }

    $remote->blocking(0);

    my $paddr;
    if (my $iaddr = inet_aton("www.yahoo.com")) {
    $paddr = sockaddr_in(80, $iaddr);
    } else {
    print STDERR "Error resolving remote addr: $@";
    }

    my $t0 = [gettimeofday];
    $remote->connect( $paddr );
    print STDOUT "connect time: " . tv_interval ( $t0) . "\r\n";

    close ($remote);
     
    Ed W, Mar 18, 2005
    #1
    1. Advertising

  2. Ed W

    Rocco Caputo Guest

    On Fri, 18 Mar 2005 00:36:40 GMT, Ed W wrote:
    > OK, yes I know this is practically an FAQ, but I can't get the win32
    > non-blocking socket examples to work on Perl 5.8 on Win XP.
    >
    > See below for my example program. Seems to me that this should show a
    > connect time of near zero seconds. However, it actually takes a time
    > roughly the same as the ping time to the server, indicating to me that
    > it's blocking until connected...


    Check ActiveState's bug tracker. This issue has come up several times
    and been written off as one of Windows' little quirks. You could try
    Cygwin perl instead...

    --
    Rocco Caputo - http://poe.perl.org/
     
    Rocco Caputo, Mar 18, 2005
    #2
    1. Advertising

  3. Ed W

    Ed W Guest

    Rocco Caputo wrote:
    > On Fri, 18 Mar 2005 00:36:40 GMT, Ed W wrote:
    >
    >>OK, yes I know this is practically an FAQ, but I can't get the win32
    >>non-blocking socket examples to work on Perl 5.8 on Win XP.
    >>
    >>See below for my example program. Seems to me that this should show a
    >>connect time of near zero seconds. However, it actually takes a time
    >>roughly the same as the ping time to the server, indicating to me that
    >>it's blocking until connected...

    >
    >
    > Check ActiveState's bug tracker. This issue has come up several times
    > and been written off as one of Windows' little quirks. You could try
    > Cygwin perl instead...
    >



    Actually, almost by chance I discovered that the last param to the ioctl
    has to be a reference param... Changing the code as follows makes it work:

    my $nonblocking = $blocking ? 0 : 1;
    ioctl($self, 0x8004667e, \$nonblocking);

    I have not seen this explicitly described anywhere, so hopefully it is
    useful to someone else if it turns up on a search...

    I still don't understand why such a trivial workaround is not part of
    INET.pm after all this time though? Seems to be a trivial patch?

    Thanks all

    Ed W
     
    Ed W, Mar 18, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendra Gunawan
    Replies:
    1
    Views:
    13,077
    Allan Herriman
    Apr 8, 2004
  2. Andre Kelmanson

    blocking i/o vs. non blocking i/o (performance)

    Andre Kelmanson, Oct 10, 2003, in forum: C Programming
    Replies:
    3
    Views:
    998
    Valentin Tihomirov
    Oct 12, 2003
  3. Tim Black
    Replies:
    1
    Views:
    1,384
    Alan Kennedy
    Aug 3, 2004
  4. nukleus
    Replies:
    14
    Views:
    913
    Chris Uppal
    Jan 22, 2007
  5. Serge Savoie
    Replies:
    4
    Views:
    292
    Serge Savoie
    Oct 1, 2008
Loading...

Share This Page