1 |
joko |
1.1 |
### Create a client that sends and receives Perl data structures. |
2 |
|
|
### Upon connecting with the server, it sends a list of things to |
3 |
|
|
### process. The server responds with a hash of a few things. Upon |
4 |
|
|
### receipt of that hash, the client displays its contents and exits. |
5 |
|
|
|
6 |
|
|
use POE; |
7 |
|
|
use POE::Filter::Reference; |
8 |
|
|
#use POE::Component::Server::TCP; |
9 |
|
|
use POE::Component::Client::TCP; |
10 |
|
|
|
11 |
|
|
POE::Component::Client::TCP->new |
12 |
|
|
( Alias => "sum_client", |
13 |
|
|
RemoteAddress => "localhost", |
14 |
|
|
RemotePort => 12345, |
15 |
|
|
Filter => "POE::Filter::Reference", |
16 |
|
|
|
17 |
|
|
# Build a request and send it. |
18 |
|
|
|
19 |
|
|
Connected => sub { |
20 |
|
|
my $heap = $_[HEAP]; |
21 |
|
|
|
22 |
|
|
my @request = |
23 |
|
|
qw( 0 2.718 3 3.1416 5 6 7 8 9 four score and seven years ago ); |
24 |
|
|
|
25 |
|
|
$heap->{server}->put( \@request ); |
26 |
|
|
}, |
27 |
|
|
|
28 |
|
|
# Receive a response, display it, and shut down the client. |
29 |
|
|
|
30 |
|
|
ServerInput => sub { |
31 |
|
|
my ( $kernel, $hash ) = @_[ KERNEL, ARG0 ]; |
32 |
|
|
|
33 |
|
|
if ( ref($hash) eq 'HASH' ) { |
34 |
|
|
print "Client received:\n"; |
35 |
|
|
foreach ( sort keys %$hash ) { |
36 |
|
|
my $value = $hash->{$_}; |
37 |
|
|
$value = "@$value" if ref($value) eq 'ARRAY'; |
38 |
|
|
printf "\t%-4s = $value\n", $_; |
39 |
|
|
} |
40 |
|
|
} |
41 |
|
|
else { |
42 |
|
|
print "Client received an unknown response type: ", ref($hash), "\n"; |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
$kernel->yield("shutdown"); |
46 |
|
|
}, |
47 |
|
|
); |
48 |
|
|
|
49 |
|
|
$poe_kernel->run(); |