&$resolve;
}
}
=item $guard = tcp_connect $host, $service, $connect_cb[, $prepare_cb]
This is a convenience function that creates a TCP socket and makes a 100%
non-blocking connect to the given C<$host> (which can be a hostname or
a textual IP address, or the string C<unix/> for UNIX domain sockets)
and C<$service> (which can be a numeric port number or a service name,
or a C<servicename=portnumber> string, or the pathname to a UNIX domain
socket).
If both C<$host> and C<$port> are names, then this function will use SRV
records to locate the real target(s).
In either case, it will create a list of target hosts (e.g. for multihomed
hosts or hosts with both IPv4 and IPv6 addresses) and try to connect to
each in turn.
If the connect is successful, then the C<$connect_cb> will be invoked with
the socket file handle (in non-blocking mode) as first and the peer host
(as a textual IP address) and peer port as second and third arguments,
respectively. The fourth argument is a code reference that you can call
if, for some reason, you don't like this connection, which will cause
C<tcp_connect> to try the next one (or call your callback without any
arguments if there are no more connections). In most cases, you can simply
ignore this argument.
$cb->($filehandle, $host, $port, $retry)
If the connect is unsuccessful, then the C<$connect_cb> will be invoked
without any arguments and C<$!> will be set appropriately (with C<ENXIO>
indicating a DNS resolution failure).
The file handle is perfect for being plugged into L<AnyEvent::Handle>, but
can be used as a normal perl file handle as well.
Unless called in void context, C<tcp_connect> returns a guard object that
will automatically abort connecting when it gets destroyed (it does not do
anything to the socket after the connect was successful).
Sometimes you need to "prepare" the socket before connecting, for example,
to C<bind> it to some port, or you want a specific connect timeout that
is lower than your kernel's default timeout. In this case you can specify
a second callback, C<$prepare_cb>. It will be called with the file handle
in not-yet-connected state as only argument and must return the connection
timeout value (or C<0>, C<undef> or the empty list to indicate the default
timeout is to be used).
Note that the socket could be either a IPv4 TCP socket or an IPv6 TCP
socket (although only IPv4 is currently supported by this module).
Note to the poor Microsoft Windows users: Windows (of course) doesn't
correctly signal connection errors, so unless your event library works
around this, failed connections will simply hang. The only event libraries
that handle this condition correctly are L<EV> and L<Glib>. Additionally,
AnyEvent works around this bug with L<Event> and in its pure-perl
backend. All other libraries cannot correctly handle this condition. To
lessen the impact of this windows bug, a default timeout of 30 seconds
will be imposed on windows. Cygwin is not affected.
Simple Example: connect to localhost on port 22.
tcp_connect localhost => 22, sub {
my $fh = shift
or die "unable to connect: $!";
# do something
};
Complex Example: connect to www.google.com on port 80 and make a simple
GET request without much error handling. Also limit the connection timeout
to 15 seconds.
tcp_connect "www.google.com", "http",
sub {
my ($fh) = @_
or die "unable to connect: $!";
my $handle; # avoid direct assignment so on_eof has it in scope.
$handle = new AnyEvent::Handle
fh => $fh,
on_eof => sub {
undef $handle; # keep it alive till eof
warn "done.\n";
};
$handle->push_write ("GET / HTTP/1.0\015\012\015\012");
$handle->push_read_line ("\015\012\015\012", sub {
my ($handle, $line) = @_;
# print response header
print "HEADER\n$line\n\nBODY\n";
$handle->on_read (sub {
# print response body
print $_[0]->rbuf;
$_[0]->rbuf = "";
});
=6= |