Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Re: Non blocking socket open

by jepri (Parson)
on Jun 29, 2001 at 12:35 UTC ( [id://92559]=note: print w/replies, xml ) Need Help??


in reply to Re: Non blocking socket open
in thread Non blocking socket open

Which according to some discussions does not appear to work under Win32 very well, or at all. YMMV.

Damn. One of my desires was to make it more cross-platform, not less.

Thanks for the link.

____________________
Jeremy
I didn't believe in evil until I dated it.

Replies are listed 'Best First'.
Re: Re: Re: Non blocking socket open
by John M. Dlugosz (Monsignor) on Jun 29, 2001 at 18:19 UTC
    Winsock has a way to do nonblocking sockets, too. Perhaps you can take that extension, and conditionally call it or use the Win32 way depending on what OS you find yourself under. Ideally that would be part of the socket extension class discussed earlier.

    The Win32 implementation of connect can return WSAEWOULDBLOCK if "The socket is marked as nonblocking and the connection cannot be completed immediately."

    —John Anecdotal P.S. For years I had a ray-traced image of a wooden block (originally a study in woodgrain texture) stuck on the wall with a caption "WSAEWOODBLOCK". One day someone came into my office and, referring to an error message he had gotten, asked what a WSAEWOULDBLOCK was. I silently pointed to the sign.

(tye)Re: Non blocking socket open
by tye (Sage) on Jun 29, 2001 at 20:22 UTC

    It wouldn't be hard to make blocking(0) work under Win32. The code to change is in the Perl source distribution in ext/IO/IO.xs. This patch needs to be sent to p5p, so if you have the motivation to find the tuits to get that done, then many, many people would be appreciative.

    See the previously noted thread and the follow up for details on solutions and patch ideas.

            - tye (but my friends call me "Tye")
      I discovered that you can actually do non-blocking connects without patching the Perl source, and fairly portably too, so long as you set up a few OS-dependant constants and subs. Here's some code I wrote a while back for just this purpose. If you ask nicely, you might just coax an explanation out of me too (that is, after I've figured out how it works again :-)
      package Portable::NonBlock; use strict; use vars qw(@ISA @EXPORT @EXPORT_OK); require Exporter; use POSIX; use Socket; use Symbol; @EXPORT = qw(); @EXPORT_OK = qw(setnonblock FIONBIO EAGAIN); @ISA = qw(Exporter); ### Default ### *setsocknonblock = sub { my $sock = shift; my $flags = fcntl($sock, F_GETFL, 0) or warn "Can't get the fcntl flags on socket: $!\n"; fcntl($sock, F_SETFL, $flags | O_NONBLOCK) or warn "Can't fcntl socket to non-blocking: $!\n"; }; ### Win32 ### sub SO_OPENTYPE { 0x7008 } if ($^O eq 'MSWin32') { *POSIX::FIONBIO = sub { 0x8004667E }; # Gleaned from VC++ winso +ck2.h (\x8004667E) *POSIX::EAGAIN = sub { 10035; }; # Gleaned by forcing a no +n-blocking send # Total *HACK* to allow winsock connect() to work on non-blocking so +ckets # Culprit is in perl source /win32/win32sck.c function set_socktype. + We undo # the result of this function. See MSDN support on overlapped I/O fo +r background # info: http://support.microsoft.com/support/kb/articles/Q181/6/11.A +SP my $sock = gensym(); socket($sock, PF_INET, SOCK_STREAM, getprotobyname('tcp')) or print "PORTABLE::BEGIN ERROR - can't create socket\n"; setsockopt($sock, SOL_SOCKET, SO_OPENTYPE, 0) or print "PORTABLE::BEGIN ERROR - Can't setsockopt to overlapped: $!\ +n"; close $sock; # Can't fcntl in windows, so we ioctlsocket. my $truevalue = 1; *setsocknonblock = sub { my $sock = shift; ioctl($sock, POSIX::FIONBIO(), \$truevalue) or warn "Can't ioctl socket to non-blocking: $!\n"; }; }
         MeowChow                                   
                     s aamecha.s a..a\u$&owag.print

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://92559]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-16 06:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found