我有一个客户端脚本来接收IPv4地址上的TCP数据包。 现在,我需要脚本在IPv6地址上接收TCP数据包。 由于我对perl-Socket了解不多,你能帮我修改一下吗?
use Socket;
use IO::Handle;
use constant MY_ECHO_PORT => 9067;
$ENV{'PATH'} = "$ENV{'PATH'}:./";
my ($bytes_out,$bytes_in) = (0,0);
my $dt;
my $port = shift || MY_ECHO_PORT;
my $protocol = getprotobyname('tcp');
local *SESSION;
local *SOCK;
$SIG{'INT'} = sub {
close(SESSION) if SESSION;
shutdown(SOCK,2) if SOCK;
close(SOCK) if SOCK;
print STDERR "Mlease agent stopped\n";
exit 0;
};
socket(SOCK, AF_INET, SOCK_STREAM, $protocol) or
die "socket() failed: $!";
setsockopt(SOCK,SOL_SOCKET,SO_REUSEADDR,1) or
die "Can't set SO_REUSEADDR: $!";
my $my_addr = sockaddr_in($port,INADDR_ANY);
bind(SOCK,$my_addr) or
die "bind() failed: $!";
listen(SOCK,SOMAXCONN) or
die "listen() failed: $!";
print "Mlease agent started on port $port...\n";
答案 0 :(得分:0)
ipv6-agent.pl ,执行收到的客户端命令并将输出发送回客户端
use strict;
use warnings;
use IO::Socket::IP -register;
use constant MY_ECHO_PORT => 9067;
my $port = shift || MY_ECHO_PORT;
my $sock = IO::Socket->new(
Domain => PF_INET6,
LocalHost => "::1",
Listen => 1,
LocalPort => $port,
Proto => "tcp",
ReuseAddr => 1,
) or die "Cannot create socket - $@\n";
$SIG{'INT'} = sub {
$sock->close() if $sock;
print STDERR "Mlease agent stopped\n";
exit 0;
};
while (1) {
my $client = $sock->accept();
# get information about a newly connected client
my $client_address = $client->peerhost();
my $client_port = $client->peerport();
print "connection from $client_address:$client_port\n";
# read up to 1024 characters from the connected client
my $data = "";
$client->recv($data, 1024);
print "received data: $data\n";
chomp $data;
my $res = qx/$data/;
$client->send($res);
# notify client that response has been sent
shutdown($client, 1);
} ## end while (1)
运行你的脚本:
perl ipv6-agent.pl
运行客户:
nc ::1 9067
date
Wed Jun 28 08:27:20 CEST 2017