1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!perl
use warnings;
use strict;
use IO::Socket;
use POE qw(Wheel::SocketFactory Wheel::ReadWrite);
my $SERVER_ADDR = '192.168.178.6';
my $SERVER_PORT = '50099';
POE::Session->create(
inline_states => {
_start => sub {
# Start the server.
$_[HEAP]{server} = POE::Wheel::SocketFactory->new(
RemoteAddress => $SERVER_ADDR,
RemotePort => $SERVER_PORT,
SocketProtocol => 'udp',
SuccessEvent => "on_connect",
FailureEvent => "on_server_error",
);
},
on_connect => sub {
# Begin interacting with the server.
my $kernel = $_[KERNEL];
my $client_socket = $_[ARG0];
my $io_wheel = POE::Wheel::ReadWrite->new(
Handle => $client_socket,
InputEvent => "on_receive_data",
ErrorEvent => "on_connect_error",
);
$_[HEAP]{client}{ $io_wheel->ID() } = $io_wheel;
$io_wheel->put( "login monitor monitor", "log on", );
$kernel->yield('keepalive', $io_wheel);
},
on_server_error => sub {
# Shut down server.
my ( $operation, $errnum, $errstr ) = @_[ ARG0, ARG1, ARG2 ];
warn "Server $operation error $errnum: $errstr\n";
delete $_[HEAP]{server};
},
on_receive_data => sub {
# Handle client input.
my ( $kernel, $input, $wheel_id ) = @_[ KERNEL, ARG0, ARG1 ];
print "Received: $input\n";
},
on_connect_error => sub {
# Handle client error, including disconnect.
my $wheel_id = $_[ARG3];
delete $_[HEAP]{client}{$wheel_id};
},
keepalive => sub {
my ( $io_wheel ) = @_[ ARG0 ];
$_[KERNEL]->delay_add( keepalive => 10 );
$io_wheel->put( "keepalive" );
},
}
);
POE::Kernel->run();
exit;