[boulder.pm] Newbie question...

Justin Crawford justin.crawford at cusys.edu
Tue Feb 8 09:00:44 CST 2000


Hi, I'm new to the list and not sure that I qualify as a Monger yet.  In any event, I've got a script question maybe someone can help with....

Trying to make a basic server program using IO::Socket.  I copied the whole script from a 'how-to' in a perl magazine.  First, here is the output (assuming a successful client connection) on the server:

AspDev/____> server
Server ready.  Waiting for connections. . .
Im starting a fork
Im too far down in the script!!
Im WAY too far down now!!

Next, the script:

AspDev/____> cat server
#!/usr/local/bin/perl -w
use strict;
use IO::Socket;
use POSIX 'WNOHANG';

use constant PORT => 42444;

$SIG{CHLD} = sub { while ( waitpid (-1, WNOHANG) > 0 ) { } };

my $listen_socket = IO::Socket::INET->new(LocalPort => PORT,
                                          Listen => 20,
                                          Proto => 'tcp',
                                          Reuse => 1);

die "Can't create a listening socket: $@" unless $listen_socket;
warn "Server ready.  Waiting for connections. . .\n";


while (my $connection = $listen_socket->accept) {
   print "Im starting a fork\n";
   die "Can't fork: $!" unless defined (my $child = fork());
   if ($child == 0) {
      $listen_socket->close;
      interact($connection);
      exit 0;
   }
}
continue {
   $connection->close;
}

print "Im too far down in the script!!\n";

sub interact {
   my $sock = shift(@_);
   print $sock "This is being printed on Delphi.\n";
}

print "Im WAY too far down now!!\n";



The question, of course, is why is the script only passing through the while-loop once?  Thanks a lot for reading!

-justin


Justin Crawford
Oracle DBA Group
University Management Systems
(303) 492-9083
justin.crawford at cusys.edu




More information about the Boulder-pm mailing list