Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Non-blocking TCP connections

by twerq (Deacon)
on Mar 22, 2001 at 00:39 UTC ( [id://66135]=CUFP: print w/replies, xml ) Need Help??

Many people have posted asking for help with a non-blocking IO::Socket::INET connection, specifically as a tcp client. As far as I could see, there was no way. . . hence this small hack. Simply use IO::Socket::INET_NONBLOCK and your sockets will be returned (true) every time, almost immediately. Which can be very useful in conjunction with IO::Select, when you have to do many connections and don't want to wait on them. . . mail me here with any success or failure reports. .
# IO::Socket::INET_NONBLOCK.pm # # Copyright (c) 2001 Derek Watson <watson@elyrium.com>. # # Cheap Hack(tm) agains Graham Barr's IO::Socket::INET # to allow non-blocking client Socket connections # package IO::Socket::INET_NONBLOCK; use strict; our(@ISA, $VERSION); use IO::Socket::INET; use Carp; @ISA = qw(IO::Socket::INET); sub connect { # this is a redifined IO::Socket::INET->connect @_ == 2 || @_ == 3 or croak 'usage: $sock->connect(NAME) or $sock->connect(PORT, ADD +R)'; # grab our socket my $sock = shift; # set to non-blocking $sock->blocking(0); # pack the host address my $addr = @_ == 1 ? shift : pack_sockaddr_in(@_); # pass directly to perl's connect() function, # bypassing the call to IO::Socket->connect # which usually handles timeouts, blocking # and error handling. connect($sock, $addr); # lets just go ahead and assume it worked. . . return 1; } 1;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-07-15 02:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.