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

Trying with my server/client stuff.

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

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; }
My executing program(just for testing):
#!/usr/bin/perl -w my $num=1; foreach(1..5) { print "PRINT: $num\n"; $num++; sleep 5; }
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...

--
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

    I think you want to call exit 0; in the child block when the child has done it's work. Alternatively, this may be a case where exec("sleep.pl"); is useful.

    As it is, I think the kids will keep piling up, each wanting to act as a new server on the same connection.

    After Compline,
    Zaxo

Re: more forking troubles
by toadi (Chaplain) on Dec 12, 2001 at 15:30 UTC
    Think I solved it myself:

    In the childe part ik put exit(0) after the exec sleep. This seems to solve the problem...

    UPDATE: Zaxo and me seem to have found the same solution on the same moment :)

    --
    My opinions may have changed,
    but not the fact that I am right