我在Windows(5.10.1的activeperl构建1006)使得在被连接到叉,接受一些JSON数据,并将其写入到数据库中创建用Perl的服务器。在64个客户端连接到服务器后,我遇到了一个问题,当尝试分叉时,错误消息是“资源不可用”。为什么Perl的Windows IO :: Socket在64连接后抱怨“资源不可用”?
这段代码运行在Linux下,我发现许多倒闭的子进程,这是通过增加对父wait()
电话解决。但是这并没有解决问题。在Linux下运行代码可以在Windows中执行64个调用。
我也转动起来的情况下,它是在服务器上的限制虚拟的Windows服务器,而是一个新的安装Perl的导致64同样连接限制。
任何想法表示赞赏。
use IO::Socket;
use Net::hostent;
use JSON;
use DBI;
use Data::Dumper;
my $port=shift || 9000;
my $clients_served = 0;
while(1){
my $server = IO::Socket::INET->new(Proto => 'tcp',
LocalPort => $port,
Listen => 1,
Reuse => 1);
die "can't setup server" unless $server;
print "[Server $0 is running]\n";
####
# wait for a client to connect
# once it has, fork to a seperate thread and
# retrieve the JSON data
####
while (my $client = $server->accept()) {
my $pid = fork();
if ($pid != 0) {
print ("Serving client " . $clients_served++ . "\n");
}else{
$client->autoflush(1);
my $JSONObject = JSON->new->ascii->pretty->allow_nonref();
my $hostinfo = gethostbyaddr($client->peeraddr);
my $client_hostname = ($hostinfo->name || $client->peerhost);
printf "connect from %s\n", $client_hostname;
print " $client_hostname connected..\n";
syswrite($client, "Reached Server\n", 2048);
if (sysread($client, my $buffer, 2048) > 0) {
foreach my $tasks($JSONObject->decode($buffer)){
foreach my $task (@$tasks){
insert_record($client_hostname, $task); #empty method, contents does not affect result
}
}
}
print " $client_hostname disconnected..\n";
close $client;
exit 0;
}
}
$server->close();
}
exit 0;
64个套接字应该足以应付任何人 - 比尔盖茨 – mob 2010-02-17 21:54:17
哪个Windows版本?非服务器版本对传入连接的数量有各种限制。 – 2010-02-17 22:19:01
我仍然对Windows如何教会人们重新安装是第一件事情之一感到惊讶。 :) – 2010-02-26 01:27:15