ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/eg/listen
Revision: 1.2
Committed: Sat May 24 17:06:10 2008 UTC (16 years, 1 month ago) by elmex
Branch: MAIN
CVS Tags: rel-4_0
Changes since 1.1: +9 -19 lines
Log Message:
updated examples

File Contents

# Content
1 #!/opt/perl/bin/perl
2 use strict;
3 use Socket;
4 use IO::Socket::INET;
5 use AnyEvent::Socket;
6 use AnyEvent::Handle;
7
8 my $cv = AnyEvent->condvar;
9
10 my $hdl;
11
12 warn "listening on port 34832...\n";
13
14 AnyEvent::Socket::tcp_server undef, 34832, sub {
15 my ($clsock, $host, $port) = @_;
16 print "Got new client connection: $host:$port\n";
17
18 $hdl =
19 AnyEvent::Handle->new (
20 fh => $clsock,
21 on_eof => sub { print "client connection $host:$port: eof\n" },
22 on_error => sub { print "Client connection error: $host:$port: $!\n" }
23 );
24
25 $hdl->push_write ("Hello!\015\012");
26
27 $hdl->push_read_line (sub {
28 my (undef, $line) = @_;
29 print "Yay, got line: $line\n";
30 $hdl->push_write ("Bye\015\012");
31 $hdl->on_drain (sub { $hdl->fh->close; undef $hdl });
32 });
33 };
34
35 $cv->wait;