Your question seems confused. Do you want to handle multiple connections in a single process, or do you want to write a forking or pre-forking server like Apache's httpd? All three are varying levels of easy.

Here's a single-process, event-driven way:

#!/usr/bin/env perl use warnings; use strict; # Note: POE's default event loop uses select(). # See CPAN for more efficient POE::Loop classes. use POE; use POE::Component::Server::TCP; POE::Component::Server::TCP->new( Port => 8888, ClientConnected => sub { print "Client connected.\n"; }, ClientInput => sub { my ($app, $storage, $input) = @_[KERNEL, HEAP, ARG0]; print "Got client input: $input\n"; $storage->{client}->put($input); $app->yield("shutdown") if $input eq "quit"; }, ClientDisconnected => sub { print "Client disconnected.\n"; }, ); POE::Kernel->run();

Here's another, adapted from Perl Cookbook recipe 17.11 (Forking Servers):

#!/usr/bin/env perl use warnings; use strict; use POSIX qw(:sys_wait_h); use IO::Socket::INET(); use IO::Handle; sub reaper { 1 until waitpid(-1, WNOHANG) == -1; $SIG{CHLD} = \&reaper; } $SIG{CHLD} = \&reaper; my $server = IO::Socket::INET->new( Listen => 5, ReuseAddr => 1, LocalPort => 8888, Proto => 'tcp' ); while (1) { my $client; my $client_addr = accept($client, $server); next unless $client_addr; # Fork to handle the connection. my $pid = fork(); # Parent should just accept again. next if $pid; # Handling errors is good. die "fork failed: $!" unless defined $pid; # Child here. close($server); $client->autoflush(1); print "$$: client connected\n"; while (<$client>) { chomp; print "$$: got input: $_\n"; print $client "$_\n"; last if /^\s*quit\s*/; } print "$$: client disconnected\n"; close $client; exit; }

Perl Cookbook recipe 17.12 (Pre-Forking Servers) is a couple pages long, so I'm not going to adapt it here. I believe the source is legally available online, and consider this incentive to buy a fine book.


In reply to Re: Event based handling of multiple connections with AnyEvent by rcaputo
in thread Event based handling of multiple connections with AnyEvent by bennymack

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.