The below code does run a TCP Server which fork a new process when receiving TCP request. Why does it exits, just after the first request accepted?
#!/usr/bin/perl
use Socket;
use POSIX qw(:sys_wait_h);
sub REAPER {
1 until -1 == waitpid(-1, WNOHANG);
$SIG{CHLD} = \&REAPER;
}
$SIG{CHLD} = \&REAPER;
socket(SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp'));
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1);
$my_addr = sockaddr_in(8080, INADDR_ANY);
bind SERVER, $my_addr;
listen(SERVER, 1000);
REQUEST:
while (accept(CLIENT, SERVER)) {
if ($pid = fork) {
close CLIENT;
next REQUEST;
}
die "$!" unless defined $pid;
close SERVER;
print CLIENT "hello\n";
close CLIENT;
exit;
}