Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I've written up a client and a server. They both work fine if I have only one client connected at a time. Here is the begining code of my server:
#!/usr/bin/perl -w use File::Copy; use IO::Socket; use Net::hostent; # for OO version of gethostbyaddr #open(CONFIG, "GremlinServerConfig.txt") || die("Can't open GremlinSer +verConfig.txt: $!"); my($resultsDir) = ".\\results"; my(@romFile, @baseState, @appName, @currentSeed, @maxSeed, @seedsPerPa +cket, @switchEvent, @maxEvent, @outputFile,@status); my($currentJob); my($CONNECTION_FAILLURE) = -1; my($SUCCESS) = 0; $PORT = 9000; # pick something not in use $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"; while ($client = $server->accept()) { $client->autoflush(1); $hostinfo = gethostbyaddr($client->peeraddr); printf "[Connect from %s]\n", $hostinfo->name || $client->peerhost +; print $client "Command? \n.\n"; while (<$client>) { next unless /\S/; # blank line if (/quit|exit/i) { last; } elsif (/date|time/i) { printf $client "%s\n", scalar localt +ime; } elsif (/request/i ) { # Check if there are any jobs to process if ($#romFile > -1) { $test = $#romFile; print $client "something there...$test\n"; HandleRequest($client); } else { print $client "All jobs finished\n"; } redo; } elsif (/results/i ) { HandleResults($client); } elsif (/add/i) { CreateProject($client); } elsif (/view/i) { PrintAll($client); } else { print $client "Commands: quit date add view\n"; } } continue { print $client "Command?\n.\n"; } }
How could I go about making this work for multiple connections? If I connect from one client, and try to connect from another, the second client is blocked until the first one quits. Is there a way to have multiple simultaneous connections? Thanks

Replies are listed 'Best First'.
(tye)Re: multiple socket connection
by tye (Sage) on Sep 25, 2001 at 22:17 UTC

    I'd look into Net::Server. It supports building a single server using select (I think) or preforking to handle multiple client requests simultaneously, as well as lots of other configurations.

            - tye (but my friends call me "Tye")
Re: multiple socket connection
by perrin (Chancellor) on Sep 25, 2001 at 22:05 UTC
    I suggest you pick up a copy of either Advanced Perl Programming or Perl Cookbook. Both have good discussions on how to build servers.

    You could also just grab one of the available CPAN modules and save yourself a lot of time.