Difference between revisions of "Perl POE"
From Teknologisk videncenter
m (→A simple multitasking TCP Server) |
m (→A simple multitasking TCP Server) |
||
Line 2: | Line 2: | ||
= POE Networking = | = POE Networking = | ||
==A simple multitasking TCP Server == | ==A simple multitasking TCP Server == | ||
− | <source lang="perl"> | + | This server is doing nothing useful, but are showing how to establish the server and control client connections. When Client connects they connect at line 14 and the server send a Greeting to the client. If the client send information to the server it is received in th '''ClientInput''' subroutine in line 18. If the Client sends '''Get orders''' it receives the content of hash '''%clientorder''' |
+ | <source lang="perl" line> | ||
#!/usr/bin/perl | #!/usr/bin/perl | ||
use warnings; | use warnings; | ||
Line 28: | Line 29: | ||
$_[HEAP]{client}->put("501 Unknown Command received from Client"); | $_[HEAP]{client}->put("501 Unknown Command received from Client"); | ||
} | } | ||
− | |||
$_[HEAP]{client}->put($client_input); | $_[HEAP]{client}->put($client_input); | ||
}, | }, | ||
Line 38: | Line 38: | ||
exit; | exit; | ||
</source> | </source> | ||
+ | |||
== A simple TCP Client == | == A simple TCP Client == | ||
<source lang="perl"> | <source lang="perl"> |
Revision as of 08:46, 22 March 2009
Contents
POE - portable multitasking and networking framework for Perl
POE Networking
A simple multitasking TCP Server
This server is doing nothing useful, but are showing how to establish the server and control client connections. When Client connects they connect at line 14 and the server send a Greeting to the client. If the client send information to the server it is received in th ClientInput subroutine in line 18. If the Client sends Get orders it receives the content of hash %clientorder
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use POE qw(Component::Server::TCP);
5
6 print "LMS";
7 my %clientorder = ( "LCnumber" => 7,
8 "LCsessions" => 10,
9 "loadServer" => "sun.tekkom.dk",
10 "loadPort" => 80);
11
12 POE::Component::Server::TCP->new(
13 Port => 1234,
14 ClientConnected => sub {
15 print "Connection from client from ",$_[HEAP]{remote_ip},"\n";
16 $_[HEAP]{client}->put("Greetings from LMS");
17 },
18 ClientInput => sub {
19 my $client_input = $_[ARG0];
20 if ($client_input =~ /Get orders/) {
21 $_[HEAP]{client}->put("Your orders: ",%clientorder);
22 } else {
23 print "Unknown Command received from Client\n";
24 $_[HEAP]{client}->put("501 Unknown Command received from Client");
25 }
26 $_[HEAP]{client}->put($client_input);
27 },
28 ClientDisconnected => sub {
29 print "client from $_[HEAP]{remote_ip} disconnected\n";
30 },
31 );
32 POE::Kernel->run;
33 exit;
A simple TCP Client
#!/usr/bin/perl -w
use strict;
use POE qw(Component::Client::TCP);
POE::Component::Client::TCP->new
( RemoteAddress => "192.168.1.100",
RemotePort => "1234",
ServerInput => sub {
my $input = $_[ARG0];
if ($input =~ "Greetings from LMS") {
$_[HEAP]{server}->put("Get orders");
} else {
print "received: ", $input,"\n";
}
}
);
POE::Kernel->run();
exit;
Timing the lifespan of a session
sub _start_handler {
$_[HEAP]{ts_start} = time();
}
sub _stop_handler {
my $time_elapsed = time() - $_[HEAP]{ts_start};
print "Session ", $_[SESSION]->ID, " elapsed seconds: $elapsed\n";
}