- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试组装一个骨架服务器(在Perl中),该服务器遵循我在Lincoln Stein(约2001年)的《使用Perl进行网络编程》中所读到的一些指导原则。我在这里拥有的是一个简单的回显服务器,该服务器为每个连接派生一个 child ,并回显收到的任何消息,直到收到终止 token 为止。
我使用的是原始版本,然后添加了新功能,例如$ SIG {CHLD}处理程序,以及在 fork 之后更多地关闭了“不必要的”文件句柄,现在它已损坏:它在连接完成后终止while()循环。 (我尝试选择性地撤消更改,但无济于事。)
这是服务器和客户端的可运行版本,它们说明了该错误。通过简单地检查代码,该问题可能很明显。如果要运行它,则可以通过输入一个句点(.)(即终止 token )来终止客户端,这将触发服务器中的错误。
服务器:
#!/usr/bin/perl
# Template for a server.
#
use warnings;
use strict;
use Carp;
use Getopt::Std;
use File::Basename;
use IO::Socket;
use Net::hostent; # for OO version of gethostbyaddr
use POSIX 'WNOHANG';
use Data::Dumper;
use 5.010;
my $program = basename $0;
my $master_pid = $$; # Master server's pid
$| = 1; # flush STDOUT buffer regularly
###############################################################################
#
# Initialize.
#
###############################################################################
my %opts;
getopts( 'hp:', \%opts );
if ( $opts{'h'} ) { # no args, or the -h arg
print <<EOF;
Usage: $program [-p port]
Where: -p port advertised port number, > 1024 (default: 2000)
EOF
exit(0);
}
my $server_port = $opts{p} || 2000;
croak "-p port omitted.\n" if !defined $server_port;
croak "port must be numeric.\n" if $server_port !~ /^[[:digit:]]+$/;
croak "port must be 1025 .. 65535.\n"
if $server_port < 1025 || $server_port > 65535;
# Set up a child-reaping subroutine for SIGCHLD
#
$SIG{CHLD} = sub {
while ( ( my $kid = waitpid(-1, WNOHANG )) > 0 ) {
}
};
###############################################################################
#
# Become a server.
#
###############################################################################
# Open the server's advertised port for incoming connections
#
my $listen_socket = IO::Socket::INET->new(
Proto => 'tcp',
LocalPort => $server_port,
Listen => SOMAXCONN,
Reuse => 1
);
croak "Can't set up listening port: $!\n" unless $listen_socket;
say "Server ready.";
# Block on accept() call until a new connection arrives
#
my $client_fh;
while ( $client_fh = $listen_socket->accept() ) {
$client_fh->autoflush(1); # turn on frequent flushing
my $hostinfo
= gethostbyaddr( $client_fh->peeraddr ); # resolve ipaddr to name
# Now that a connection is established, spawn a conversation.
#
defined (my $child_pid = fork())
or croak "Can't fork: $!\n";
if ( $child_pid == 0 ) { # if being run by the forked child
# S T A R T O F C H I L D C O N T E X T
#
conversate($client_fh); # run the child process
#
# E N D O F C H I L D C O N T E X T
}
$client_fh->close; # Parent immediately closes its copy
}
say "Bummer - for some reason the socket->accept() failed.";
###############################################################################
#
# S U B R O U T I N E S
#
###############################################################################
# conversate ( client_fh )
#
# S T A R T O F C H I L D P R O C E S S
#
sub conversate {
my $client_fh = shift; # connection to client
$listen_socket->close; # we don't need our copy of this
my $child_pid = $$; # get our new pid
print $client_fh "READY\n"; # tell them we're here
EXCHANGE:
while (1) {
# Let client talk first
#
my $line = <$client_fh>; # ?? Isn't there an OO way?
if ( !defined $line ) {
last EXCHANGE;
}
chomp $line;
last EXCHANGE if $line eq '.';
# Now send a reply (echo) and close the connection.
#
print $client_fh "$line\n"; # ?? Isn't there an OO way?
}
exit 0; # child process exits
}
#
# E N D O F C H I L D P R O C E S S
#!/usr/bin/perl
#
use warnings;
use strict;
use Getopt::Std;
use Data::Dumper;
use File::Basename;
use 5.010;
#sub say { print "@_\n"; }
my $program = basename $0;
my %opts;
getopts( 'hvs:p:', \%opts );
if ( $opts{'h'} ) { # -h arg
print <<EOF;
Usage: $program [-v] [-s hostname [-p port]]
Where:
-s hostname host name (default: localhost)
-p port port number (default: 2000)
-v verbose mode
EOF
exit;
}
my $verbose = $opts{v} || 0;
my $hostname = $opts{s} || 'localhost'; # hard coded for now
my $port = $opts{p} || 2000;
###############################################################################
#
# Initialize
#
###############################################################################
# Initialize the ReadLine terminal
#
use Term::ReadLine;
my $term = Term::ReadLine->new($0);
###############################################################################
#
# Contact server and begin main loop
#
###############################################################################
use IO::Socket;
my $remote = IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => $hostname,
PeerPort => $port,
) or die "Cannot connect to $hostname:$port";
my $line;
EXCHANGE:
while (1) {
# Wait for server
#
$line = <$remote>;
last EXCHANGE if !defined $line; # connection closed by remote?
# Print server response
#
chomp $line;
say "SERVER: $line";
# Read from STDIN
#
$line = $term->readline("Enter something: ");
chomp $line;
# Send to server
#
print $remote "$line\n";
}
close $remote or die "Close failed: $!";
print "\n$program exiting normally.\n\n";
exit;
最佳答案
那么返回什么错误呢?检查$!
。
我敢打赌accept
被SIGCHLD
打断了。程序将控制权移交给OS后无法处理信号,因此要给您的程序一个机会信号,并在引发带有处理程序的信号时阻止系统调用返回(错误EINTR
)。
一旦您的处理程序处理了信号(这在您甚至没有注意到accept
返回之前发生),您就可以简单地重新启动accept
。换句话说,您可以通过如下编写循环来解决此问题:
while (1) {
my $client_fh = $listen_socket->accept();
if (!$client_fh) {
redo if $!{EINTR};
last;
}
...
}
$!
的信号处理程序:
local ( $!, $^E, $@ );
关于perl - Perl fork 服务器测试程序-accept()失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14045384/
如果我的 Perl 程序使用 Perl 模块,它将如何确定在哪里可以找到包含模块代码的文件? 例如,如果程序包含: use MyModule1; # Example 1 us
我在一个文件中有一些不同格式的数字:8.3、0.001、9e-18。我正在寻找一种简单的方法来读取它们并存储它们而不会损失任何精度。这在 AWK 中很容易,但在 Perl 中是如何完成的呢?我只愿意使
我在一个文件中有一些不同格式的数字:8.3、0.001、9e-18。我正在寻找一种简单的方法来读取它们并存储它们而不会损失任何精度。这在 AWK 中很容易,但在 Perl 中是如何完成的呢?我只愿意使
我正在自学 Perl,并且在我的 Windows 8 64 位系统上安装了 Strawberry。 Strawberry 命令行似乎工作正常,我在 C 驱动器上的 Strawberry 文件夹中创建了
我在 Perl 模块 IO::Socket::SSL 中发现了一个错误,我可能会修复它,但是,我担心测试修复。我从 Debian 下载了源码包(因为我打算为它制作一个 Debian 包或补丁)并查看了
我有一个 perl 文件,它使用了两个 perl 模块 A.pm 和 B.pm。 但是在 B.pm 中我需要调用 A.pm 的子程序。即使我在 A.pm 中使用并尝试使用它,我仍然遇到未定义的错误。
有没有办法在 Perl 运行时加载整个模块?我原以为我用 autouse 找到了一个很好的解决方案,但以下代码无法编译: package tryAutouse2; use autouse 'tryAu
过去,我编写过许多 perl 模块,以及不止一些独立的 perl 程序,但我之前从未发布过多文件 perl 程序。 我有一个几乎处于 beta 阶段的 perl 程序,它将被开源发布。它需要一些数据文
我有 1 个 perl 脚本,我们在其中编写了几个子例程。例子: # Try_1.pl main(); sub main{ --- --- check(); } check { -- --} 现在,
似乎 CPAN 上的一些(很多?)模块部分是使用 XS 在 C 中实现的,如果需要,可以回退到纯 perl 实现。虽然这很聪明,但它显然会损害性能,我想知道它是否会发生,以便我可以解决问题。 有没有一
我对 perl 很陌生。我希望我可以从 perl 安装一些软件包,我这样做是这样的: perl -MCPAN -e 'install VM::EC2' 我猜它由于依赖而失败,它显示: Result:
给定一个 Perl 包 Foo.pm,例如 package Foo; use strict; sub bar { # some code here } sub baz { # more
我有一个用 Perl 编写的测试生成器。它生成连接到模拟器的测试。这些测试本身是用 Perl 编写的,并通过其 API 连接到模拟器。我希望生成的代码是人类可读的,这意味着我希望它能够正确缩进和格式化
我正在学习 Perl,非常新的用户。我可以知道这些 Perl 代码之间有什么区别吗? #!/usr/bin/perl & #!/usr/bin/perl -w 最佳答案 那不是 perl 代码,它是
我不认为这是一个重复的问题。这专门针对 Perl 模块附带的脚本。 通常,在安装多个 Perl 版本时,您可以将 perl 可执行文件标记为版本号 (perl5.32),这样它们就可以在 /whate
我有一个在文件中使用 Blowfish 加密的程序和第二个 perl 程序,它提示输入用于将其解密为字符串的密码,我希望不必将解密的源代码写入硬盘驱动器,尽管将它放在内存中并不是真正的问题,因为运行程
有没有人为 Perl 中的惰性求值列表找到了一个好的解决方案?我尝试了很多方法来改变类似的东西 for my $item ( map { ... } @list ) { } 进入懒惰的评估——例如,通
我安装了多个版本的 Perl。 我已经指定了要使用的版本。但是为了验证,我想从 .pl 脚本本身输出 Perl 的版本。 这可能吗? 在 Perl 脚本中解析“perl --version”的输出似乎
人们还经常问“我怎样才能编译 Perl?”而他们真正想要的是创建一个可以在机器上运行的可执行文件,即使他们没有安装 Perl。 我知道有几种解决方案: perl2exe靛蓝之星 它是商业的。我从未尝试
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 8年前关闭。 Improve this
我是一名优秀的程序员,十分优秀!