- 论坛徽章:
- 0
|
在poe,AnyEvent都试了下,比如:
1、在客户端发送多个信息,下面服务端如果没有push_send,只能接受一次信息。
use AnyEvent::Handle::UDP;
use IO::Socket::INET;
my $cv_recv = AnyEvent->condvar;
my $server = AnyEvent::Handle::UDP->new(bind => ['127.0.0.1', 20203], on_recv => \&my_recv);
print "h\n";
$cv_recv->recv;
sub my_recv{
my ($message, $handle, $client_addr) = @_;
print $message,"\n";
$handle->push_send("World", $client_addr);
}
2、下面这个也类似,如果去掉了send,也只能打印一次接收的信息。
use POE;
use IO::Socket::INET;
use constant DATAGRAM_MAXLEN => 1024;
POE::Session->create(
inline_states => {
_start => \&server_start,
get_datagram => \&server_read,
}
);
POE::Kernel->run();
exit;
sub server_start {
my $kernel = $_[KERNEL];
my $socket = IO::Socket::INET->new(
Proto => 'udp',
LocalPort => '20203',
);
die "Couldn't create server socket: $!" unless $socket;
$kernel->select_read($socket, "get_datagram");
}
sub server_read {
my ($kernel, $socket) = @_[KERNEL, ARG0];
my $remote_address = recv($socket, my $message = "", DATAGRAM_MAXLEN, 0);
return unless defined $remote_address;
my ($peer_port, $peer_addr) = unpack_sockaddr_in($remote_address);
my $human_addr = inet_ntoa($peer_addr);
print "(server) $human_addr : $peer_port sent us $message\n";
$message =~ tr[a-zA-Z][n-za-mN-ZA-M];
send($socket, $message, 0, $remote_address) == length($message)
or warn "Trouble sending response: $!";
} |
|