If you're using POE, you can have both a server and its client in the same program at the same time. You are not limited to one or the other. For example:
#!perl
use warnings;
use strict;
use POE qw(Component::Server::TCP Component::Client::TCP);
POE::Component::Server::TCP->new(
Alias => "server",
Port => 12345,
ClientConnected => sub {
print "server got a connection from $_[HEAP]{remote_ip}\n";
$_[HEAP]{client}->put("Smile from the server!");
},
ClientInput => sub {
my $client_input = $_[ARG0];
print "server got client input: $client_input\n";
$client_input =~ tr[a-zA-Z][n-za-mN-ZA-M];
$_[HEAP]{client}->put($client_input);
},
);
POE::Component::Client::TCP->new(
RemoteAddress => "localhost",
RemotePort => 12345,
Connected => sub {
print "client connected to server\n";
$_[HEAP]{server}->put("smile");
},
ServerInput => sub {
my $input = $_[ARG0];
print "client received from server: $input\n";
if ($input eq "fzvyr") { # rot13(smile)
$_[KERNEL]->yield("shutdown");
$_[KERNEL]->post(server => "shutdown");
}
},
);
POE::Kernel->run();
exit;
The output looks something like this:
server got a connection from 127.0.0.1
client connected to server
server got client input: smile
client received from server: Smile from the server!
client received from server: fzvyr