in reply to Creating a server that accepts more than one client.

The usual way to do this is to have a parent process that is essentially always blocked on accept, with child processes that do the actual work. Fitting this into your framework would be something like this:

while ($client = $server->accept()) { $pid = fork(); if ($pid == 0) { print $client "Welcome to $0"; # etc. # Probably a loop here reading from $client # ... Other stuff exit 0; # Remember to go away when done! } # parent process here close $client; } # and go back to accept.
One thing missing from here is a throttle. You want to keep a count of active child processes so you don't keep creating new ones without any limit. Incrementing the counter is easy; the trick is to decrement the counter properly when each child process exits. In unix-land you can trap SIGCHLD for that, I don't know the equivalent on Win32. Whichever method you use, be careful to single-thread access to the counter. In other words, make sure when two children exit "at the same time", the counter really is decremented twice.

I haven't looked at the modules for handling forks. They may well solve this problem for you.

HTH

Replies are listed 'Best First'.
Re: Re: Creating a server that accepts more than one client.
by traveler (Parson) on Aug 17, 2001 at 00:55 UTC
    Building on this, there are two kinds of servers for multiple clients: 1) multiple simultaneous clients and, 2) multpile sequential clients. Looking at your code, it appears that you have a server (at least trying to be) of the second type. The "while(accept)fork" model as explained by VSarkiss is for multiple simultaneous clients.

    The Net::Server module looks very interesting. I'd never seen or heard of it before, but may go that way in the future, if it is possible. Unfortunately such modules are not always compatible with other modules (e.g. Gtk becuase it does not use Gtk's event model), so you sometimes have to write your own.

    HTH, --traveler