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