i have this script (to learn about fork... a simple chat server)

#!/usr/bin/perl -w
use IO::Socket;
use Net::hostent;              # for OO version of gethostbyaddr
$PORT = 9000;                  # pick something not in use
use IO::Handle;     # thousands of lines just for autoflush :-(
     pipe(PARENT_RDR, CHILD_WTR);                # XXX: failure?
     pipe(CHILD_RDR,  PARENT_WTR);               # XXX: failure?
     CHILD_WTR->autoflush(1);
     PARENT_WTR->autoflush(1);
$server = IO::Socket::INET->new( Proto     => 'tcp',
                                  LocalPort => $PORT,
                                  Listen    => SOMAXCONN,
                                  Reuse     => 1);
die "can't setup server" unless $server;
print "[Server $0 accepting clients]\n";

$SIG{"CHLD"} = "IGNORE";
for(;;) {
$client = $server->accept();
if ($pid = fork()) { ##fork 1
        close $client;
        waitpid($pid,0); ### line 19!!! ##################
} else {
   $client->autoflush(1);
   print $client "Welcome to $0\n\r";
   $hostinfo = gethostbyaddr($client->peeraddr);
   printf "[Connect from %s]\n", $hostinfo->name || $client->peerhost;
   if ($pid2 = fork()) { ## fork 2
       close PARENT_RDR;
       close PARENT_WTR;
     while (<$client>) {

       my $line = $_;
       print CHILD_WTR "$$: $line";
       $line = <CHILD_RDR>;
       print $client "\r$line";
     }
     waitpid($pid2,0); ###line 38
   } else {
       close $client;
       close CHILD_WTR;
       close CHILD_RDR;
     for(;;) {    ## this loop here is being infinit but i will change that

       my $line = <PARENT_RDR>;
       print PARENT_WTR "$line";
     }
   }
   close $client;
   exit(1);
}
}

here's the question:
when i comment (#) line 19 the server accepts clients (more than 1) but the 
second fork() does not work
with line 19 commented the second fork() works but the server just accept 1 
client

cant anyone help me with this script?
________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com


---
You are currently subscribed to perl-win32-users as: [archive@jab.org]
To unsubscribe, forward this message to
         [EMAIL PROTECTED]
For non-automated Mailing List support, send email to  
         [EMAIL PROTECTED]

Reply via email to