toadi has asked for the wisdom of the Perl Monks concerning the following question:
Problem:
If I have a connect from a client, a program needs to be executed. each time when there is a connect from a client. But if the program isn't finished executing it needs to wait until it's finished and then start it...
My solution:
SERVER: #!/usr/bin/perl -w
My executing program(just for testing):use IO::Socket; use Net::hostent; $PORT = 9000; $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); my $pid = fork(); if ($pid) { #parent my $hostinfo = gethostbyaddr($client->peeraddr); printf "[Connect from %s]\n", $hostinfo->name || $client->peerhost +; print "Being Handled by child with pid $pid]\n"; wait(); } else { #child print $client "Welcome to $0.\n"; system('./sleep.pl'); } close $client; }
But there is still a problem here :( when I telnet to the server the telent session keeps hanging after being finished. Si I assume if I write a client it also keeps hanging...#!/usr/bin/perl -w my $num=1; foreach(1..5) { print "PRINT: $num\n"; $num++; sleep 5; }
--
My opinions may have changed,
but not the fact that I am right
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: more forking troubles
by Zaxo (Archbishop) on Dec 12, 2001 at 15:29 UTC | |
|
Re: more forking troubles
by toadi (Chaplain) on Dec 12, 2001 at 15:30 UTC |