in reply to Opening multiple forking servers

I'm not sure if you can make it more simple than the following:

package Foo; use base qw(Net::Server::Fork); my @ports = qw(20203 20204 20205); my @dbs = qw(tom jim jane); my %p_map; @p_map{@ports} = @dbs; Foo->run(port => \@ports); sub process_request { my $self = shift; my $port = $self->get_property('sockport'); my $db = $p_map{$port}; print "Welcome! You connected on port $port - your db is $db\n"; # run default echo server $self->SUPER::process_request(@_); }

If you are really opposed to Net::Server you could always read through its guts to see what it is doing and roll your own. Or keep your life simple. Enjoy!

Update: I guess really though this example is cheating - I haven't started three servers. I've only started one server that is listening on three ports. But I think it would be much much easier to maintain.

Update 2: Looking at your code you want a PreFork server. Well then you'd need to do the following:
use base qw(Net::Server::PreForkSimple); Foo->run( max_servers => 5, # from jwlewis sample code max_requests => 5, port => \@ports, );

my @a=qw(random brilliant braindead); print $a[rand(@a)];

Replies are listed 'Best First'.
Re^2: Opening multiple forking servers
by jalewis2 (Monk) on Mar 06, 2007 at 17:09 UTC
    Thanks for the post, I think this is what I was really after. I should be able to fit my server into the Net::Server framework.