ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/eg/listen
(Generate patch)

Comparing AnyEvent/eg/listen (file contents):
Revision 1.1 by elmex, Thu May 15 13:32:19 2008 UTC vs.
Revision 1.2 by elmex, Sat May 24 17:06:10 2008 UTC

1#!/opt/perl/bin/perl 1#!/opt/perl/bin/perl
2use strict; 2use strict;
3use Socket; 3use Socket;
4use IO::Socket::INET; 4use IO::Socket::INET;
5use AnyEvent::Util; 5use AnyEvent::Socket;
6use AnyEvent::Handle; 6use AnyEvent::Handle;
7 7
8my $cv = AnyEvent->condvar; 8my $cv = AnyEvent->condvar;
9 9
10my $sock = IO::Socket::INET->new (
11 Listen => 5, ReuseAddr => 1, LocalPort => 124,
12) or die "Couldn't make socket: $!\n";
13
14print "Listening on addres: " . $sock->sockhost . ", port: " . $sock->sockport . "\n";
15
16my $hdl; 10my $hdl;
17 11
18my $watchobj = AnyEvent::Util::listen ($sock, sub { 12warn "listening on port 34832...\n";
13
14AnyEvent::Socket::tcp_server undef, 34832, sub {
19 my ($clsock, $paddr) = @_; 15 my ($clsock, $host, $port) = @_;
20 my ($port, $addr) = sockaddr_in ($paddr);
21 $addr = inet_ntoa ($addr);
22 print "Got new client connection: $addr:$port\n"; 16 print "Got new client connection: $host:$port\n";
23 17
24 $hdl = 18 $hdl =
25 AnyEvent::Handle->new ( 19 AnyEvent::Handle->new (
26 fh => $clsock, 20 fh => $clsock,
27 on_eof => sub { print "client connection $addr:$port: eof\n" }, 21 on_eof => sub { print "client connection $host:$port: eof\n" },
28 on_error => sub { print "Client connection error: $addr:$port: $!\n" } 22 on_error => sub { print "Client connection error: $host:$port: $!\n" }
29 ); 23 );
30 24
31 $hdl->push_write ("Hello!\015\012"); 25 $hdl->push_write ("Hello!\015\012");
32 26
33 $hdl->push_read_line (sub { 27 $hdl->push_read_line (sub {
34 my (undef, $line) = @_; 28 my (undef, $line) = @_;
35 print "Yay, got line: $line\n"; 29 print "Yay, got line: $line\n";
36 $hdl->push_write ("Bye\015\012"); 30 $hdl->push_write ("Bye\015\012");
37 $hdl->on_drain (sub { $hdl->fh->close; undef $hdl }); 31 $hdl->on_drain (sub { $hdl->fh->close; undef $hdl });
38 }); 32 });
39 33};
40}, sub {
41 warn "Got error on connect: $!\n";
42 $cv->broadcast;
43}, 10);
44 34
45$cv->wait; 35$cv->wait;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines