1 |
#!/usr/bin/perl |
2 |
|
3 |
use warnings; |
4 |
use strict; |
5 |
|
6 |
use IO::Socket::INET; |
7 |
use IO::Select; |
8 |
use Storable qw(); |
9 |
use Data::Dump qw(dump); |
10 |
|
11 |
my $port = shift @ARGV; |
12 |
|
13 |
my $sock = IO::Socket::INET->new( |
14 |
PeerAddr => '127.0.0.1', |
15 |
PeerPort => $port, |
16 |
Proto => 'tcp', |
17 |
) || die $!; |
18 |
|
19 |
my $sel = IO::Select->new($sock); |
20 |
$sel->add($sock); |
21 |
|
22 |
while (1) { |
23 |
for my $sock ($sel->can_read(1)) { |
24 |
my $request = Storable::fd_retrieve($sock); |
25 |
warn "[$port] <<<< ", dump($request), $/; |
26 |
my $response = { port => $port }; |
27 |
$response->{ping} = 'pong' if $request->{ping}; |
28 |
Storable::store_fd( $response, $sock ); |
29 |
warn "[$port] >>>> ", dump($response), $/; |
30 |
} |
31 |
} |