1 |
elmex |
1.1 |
#!/opt/perl/bin/perl |
2 |
|
|
use strict; |
3 |
|
|
use Socket; |
4 |
|
|
use IO::Socket::INET; |
5 |
elmex |
1.2 |
use AnyEvent::Socket; |
6 |
elmex |
1.1 |
use AnyEvent::Handle; |
7 |
|
|
|
8 |
|
|
my $cv = AnyEvent->condvar; |
9 |
|
|
|
10 |
elmex |
1.2 |
my $hdl; |
11 |
elmex |
1.1 |
|
12 |
elmex |
1.2 |
warn "listening on port 34832...\n"; |
13 |
elmex |
1.1 |
|
14 |
elmex |
1.2 |
AnyEvent::Socket::tcp_server undef, 34832, sub { |
15 |
|
|
my ($clsock, $host, $port) = @_; |
16 |
|
|
print "Got new client connection: $host:$port\n"; |
17 |
elmex |
1.1 |
|
18 |
|
|
$hdl = |
19 |
|
|
AnyEvent::Handle->new ( |
20 |
|
|
fh => $clsock, |
21 |
elmex |
1.2 |
on_eof => sub { print "client connection $host:$port: eof\n" }, |
22 |
|
|
on_error => sub { print "Client connection error: $host:$port: $!\n" } |
23 |
elmex |
1.1 |
); |
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 |
elmex |
1.2 |
}; |
34 |
elmex |
1.1 |
|
35 |
|
|
$cv->wait; |