我尝试使用 Perl 创建 TCP 服务器。我能够成功创建 TCP 服务器来满足客户端请求。但是由于子进程已失效,我面临一个问题。完成执行后,所有子进程都将失效。我无法解决问题。
my $server = IO::Socket::INET->new
(
LocalAddr => 'localhost',
LocalPort => 48005,
Type => SOCK_STREAM,
Reuse => 1,
Listen => 5
) or die "could not open port\n";
warn "server ready waiting for connections..... \n";
my $client;
while ($client = $server->accept())
{
my $pid;
while (not defined ($pid = fork()))
{
sleep 5;
}
if ($pid)
{
close $client;
}
else
{
$client->autoflush(1);
close $server;
&printClient();
}
}
sub printClient
{
warn "client connected to pid $$\n";
print $client "Test";
exit 0;
}