Hi. I've been trying to write simple server.
In this code client connects to server, sends some data and disconnects -> child process does 'exit 0'. However, parent gets SIGCHLD and exit too. My problem is that I want parent to listen at port 6677 forever...
What's wrong with this code?
Thanks in advance
Code:
#!/usr/bin/perl
use strict;
use IO::Socket;
use IO::Select;
my $main = new IO::Socket::INET (LocalHost => 'localhost',
LocalPort => 6677,
Listen => 5,
Proto => 'tcp',
Reuse => 1
);
die unless $main;
$SIG{'CHLD'} = sub {
wait;
};
while ($chld = $main->accept()) {
my $pid = fork();
die unless defined $pid;
if ($pid == 0) {
my $buf;
print $chld "Hello world!\n";
my $select = new IO::Select(\*STDIN, $chld);
my @ready;
while (@ready = $select->can_read(180)) {
foreach (@ready) {
my ($msg, $str);
sysread($_, $msg, 2048);
unless ($msg) {
print "Quit\n";
exit 0;
# ---> here <---
}
}
}
exit 0;
}
}
In this code client connects to server, sends some data and disconnects -> child process does 'exit 0'. However, parent gets SIGCHLD and exit too. My problem is that I want parent to listen at port 6677 forever...
What's wrong with this code?
Thanks in advance