Non-blocking socket connect examples on win32 not working

E

Ed W

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);
 
R

Rocco Caputo

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...
 
E

Ed W

Rocco said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top