Perl IO::异步并行tcp连接
我有一个问题,由于某种原因,我很难找到解决办法。 我尝试通过IO::Async构建到服务器的多个并行TCP连接 我的目标是并行运行TCP连接。这些连接不需要在它们之间进行通信,但我需要捕获它们的输出并将其保存在散列中 下面的代码是一个具有单个连接的示例Perl IO::异步并行tcp连接,perl,parallels,Perl,Parallels,我有一个问题,由于某种原因,我很难找到解决办法。 我尝试通过IO::Async构建到服务器的多个并行TCP连接 我的目标是并行运行TCP连接。这些连接不需要在它们之间进行通信,但我需要捕获它们的输出并将其保存在散列中 下面的代码是一个具有单个连接的示例 #!/usr/bin/perl use strict; use warnings; use IO::Async::Loop; use IO::Async::Stream; my $CRLF = "\x0d\x0a"; # because \
#!/usr/bin/perl
use strict;
use warnings;
use IO::Async::Loop;
use IO::Async::Stream;
my $CRLF = "\x0d\x0a"; # because \r\n is not portable
my $HOST = shift @ARGV or die "Need HOST";
my $PORT = shift @ARGV or die "Need PORT";
my $loop = IO::Async::Loop->new;
my $socket;
$loop->connect(
host => $HOST,
service => $PORT,
socktype => 'stream',
on_connected => sub { $socket = shift },
on_resolve_error => sub { die "Cannot resolve - $_[0]\n" },
on_connect_error => sub { die "Cannot connect\n" },
);
$loop->loop_once until defined $socket;
# $socket is just an IO::Socket reference
my $peeraddr = $socket->peerhost . ":" . $socket->peerport;
print STDERR "Connected to $peeraddr\n";
# We need to create a cross-connected pair of Streams. Can't do that
# easily without a temporary variable
my ( $socketstream, $stdiostream );
$socketstream = IO::Async::Stream->new(
handle => $socket,
on_read => sub {
my ( undef, $buffref, $eof ) = @_;
while( $$buffref =~ s/^(.*)$CRLF// ) {
$stdiostream->write( $1 . "\n" );
}
return 0;
},
on_closed => sub {
print STDERR "Closed connection to $peeraddr\n";
$stdiostream->close_when_empty;
},
);
$loop->add( $socketstream );
$stdiostream = IO::Async::Stream->new_for_stdio(
on_read => sub {
my ( undef, $buffref, $eof ) = @_;
while( $$buffref =~ s/^(.*)\n// ) {
$socketstream->write( $1 . $CRLF );
}
return 0;
},
on_closed => sub {
$socketstream->close_when_empty;
},
);
$loop->add( $stdiostream );
$loop->await_all( $socketstream->new_close_future, $stdiostream->new_close_future );
如何修改此代码以将IP列表作为异步连接处理,并将输出存储在专用哈希中?
最后,可能将最大并行连接限制为100。
有什么想法吗?这是您的客户代码。服务器端呢?服务器站点不是很相关,但服务器可以处理并行连接。问题是,你能在这里粘贴服务器端代码吗?不幸的是,不是,但与telnet服务器相同。