Hi,
I got some problem with socket and fork. I'm running my script
on Activeperl5.6 on xp command box. I copy most of the code from
Lincoln's book on Network programming with Perl. But my code
strill can't work after fork.
--------------------
....some other definitions here....
........
my $quit = 0;
$SIG{CHLD}= sub {
my $pid;
while ($pid = waitpid(-1, WNOHANG) > 0) {
warn "Reaped pid = $pid\n";
}
};
$SIG{INT} = sub { $quit++ };
my $listen_socket = IO::Socket::INET->new(
LocalPort => PORT,
Listen => 20,
Proto => 'tcp',
Reuse => 1,
Timeout => 0,
);
die "Can't create a listening socket : $@" unless $listen_socket;
warn "Server ready. Waiting for connections...\n";
$listen_socket->blocking(0);
my $connection;
my $child;
while (! $quit) {
next unless $connection = $listen_socket->accept;
defined ($child = fork()) or warn "Can't fork : $!\n";
print "New connection: ",
<=== this is line 62
$connection->sockhost(), ":", $connection->sockport(),
" <=> ",
$connection->peerhost(), ":", $connection->peerport(),
"\n";
if ($child == 0) {
$listen_socket->close;
process($connection);
exit 0;
} else {
print "Fork pid = $child...\n";
}
$connection->close;
}
1;
sub process {
.......
--------------------
So I expect both parent and child process will print a line like this after
'fork':
New connection: ???.???.???.???:??? <=> .....
Which can make me confirm that both process shares the sock connections after
fork.
Actually the result is like this:
-----------------------
Server ready. Waiting for connections...
New connection: 127.0.0.1:9119 <=> 127.0.0.1:4709
Fork pid = -5740...
Use of uninitialized value in print at cwcd.pl line 62.
Use of uninitialized value in print at cwcd.pl line 62.
Use of uninitialized value in print at cwcd.pl line 62.
Use of uninitialized value in print at cwcd.pl line 62.
-----------------------
So why the child process can't get a valid '$connection'?
Thanks,
Xiaofang Zhou
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>