in reply to Wait for next
However, under that senario, a client will connect and his connection will be open, but will not receive a greeting from the server until it is his turn.
An alternate solution would be to use something like the Net::Server or Net::Daemon modules. These modules are intended for handling all of the server back end for you and you simply have to deal with handling the dialog with the client. To handle only having one sleep.pl run at a time, you could use file locking to assure that only one process gets it.
For example:
#!/usr/bin/perl package MyServer; use Net::Server::Fork; use Fcntl qw(LOCK_EX); @ISA = qw(Net::Server::Fork); MyServer->run(port => 20203); exit; sub process_request { my $self = shift; print STDOUT "Welcome to my server\r\n"; # while (<STDIN>){ # print "You said: $_"; # last if /quit/; # } print "Waiting to get lock on file\r\n"; my $lockfile = '/tmp/foo'; open(LOCK, $lockfile) || die "Couldn't open [$!]"; flock(LOCK,LOCK_EX) || die "Couldn't get lock [$!]"; print "Got lock, proceeding\r\n"; system('sleep.pl'); close(LOCK); print "All done, goodbye\r\n"; }
There are many ways to do it. Just depends on what you need.
my @a=qw(random brilliant braindead); print $a[rand(@a)];
|
|---|