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;