The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

AnyEvent::Socket - Connecting sockets for non-blocking I/O

SYNOPSIS

   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;

DESCRIPTION

AnyEvent::Socket provides method to connect sockets and accept clients on listening sockets.

EXAMPLES

See the eg/ directory of the AnyEvent distribution for examples and also the tests in t/handle/ can be helpful.

METHODS

new (%args)

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:

on_connect => $cb

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:

ENXIO

When the DNS lookup failed.

ETIMEDOUT

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.

on_accept

This sets the on_accept callback by calling the on_accept method. See also below.

on_accept ($cb)

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>).

AUTHOR

Robin Redeker, <elmex at ta-sa.org>

1 POD Error

The following errors were encountered while parsing the POD:

Around line 98:

Expected text after =item, not a bullet