AnyEvent::Socket - Connecting sockets for non-blocking I/O
use AnyEvent; use AnyEvent::Socket; my $cv = AnyEvent->condvar; my $ae_sock = AnyEvent::Socket->new ( PeerAddr => "www.google.de:80", on_eof => sub { $cv->broadcast }, on_connect => sub { my ($ae_sock, $error) = @_; if ($error) { warn "couldn't connect: $!"; return; } else { print "connected to ".$ae_sock->fh->peerhost.":".$ae_sock->fh->peerport."\n"; } $ae_sock->on_read (sub { my ($ae_sock) = @_; print "got data: [".${$ae_sock->rbuf}."]\n"; $ae_sock->rbuf = ''; }); $ae_sock->write ("GET / HTTP/1.0\015\012\015\012"); } ); $cv->wait;
AnyEvent::Socket provides method to connect sockets and accept clients on listening sockets.
See the eg/ directory of the AnyEvent distribution for examples and also the tests in t/handle/ can be helpful.
eg/
t/handle/
The constructor gets the same arguments as the IO::Socket::INET constructor. Except that blocking will always be disabled and the hostname lookup is done by AnyEvent::Util::inet_aton before the socket (currently a IO::Socket::INET instance) is created.
Additionally you can set the callbacks that can be set in the AnyEvent::Handle constructor and these:
Installs a connect callback, that will be called when the name was successfully resolved and the connection was successfully established or an error occured in the lookup or connect.
The first argument to the callback $cb will be the AnyEvent::Socket itself and the second is either a true value in case an error occured or undef. The variable $! will be set to one of these values:
$cb
$!
When the DNS lookup failed.
When the connect timed out.
Or any other errno as set by IO::Socket::INET when it's constructor failed or the connection couldn't be established for any other reason.
This sets the on_accept callback by calling the on_accept method. See also below.
on_accept
When the socket is run in listening mode (the Listen argument of the socket is set) this callback will be called when a new client connected. The first argument to the callback will be the AnyEvent::Socket object itself, the second the AnyEvent::Handle of the client socket and the third is the peer address (depending on what accept of IO::Socket gives you>).
Listen
accept
Robin Redeker, <elmex at ta-sa.org>
<elmex at ta-sa.org>
1 POD Error
The following errors were encountered while parsing the POD:
Expected text after =item, not a bullet
To install AnyEvent, copy and paste the appropriate command in to your terminal.
cpanm
cpanm AnyEvent
CPAN shell
perl -MCPAN -e shell install AnyEvent
For more information on module installation, please visit the detailed CPAN module installation guide.