gpt4 book ai didi

perl - 如何使脚本在IPv6地址上工作

转载 作者:行者123 更新时间:2023-12-03 12:09:22 24 4
gpt4 key购买 nike

我有一个客户端脚本来接收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";

最佳答案

基于IO::Socket::IP 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

关于perl - 如何使脚本在IPv6地址上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44772224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com