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;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.