#!/usr/bin/perl use HTTP::Daemon; use Data::Dumper; my $d = HTTP::Daemon->new( LocalAddr => 'localhost', LocalPort => 4242, ReuseAddr => 1 ) || die; my $cnt; # response loop while (my $c = $d->accept) { my $pid = fork(); # We are going to close the new connection on one of two conditions # 1. The fork failed ($pid is undefined) # 2. We are the parent ($pid != 0) if(!defined $pid || $pid == 0 ) { $c->close; print "Needs close: $pid\n"; next; } # From this point on, we are the child. # $c->close; # Close the listening socket (always done in children) # Handle requests as they come in while (my $request = $c->get_request) { print "Request:\n".Dumper($request); my $response = HTTP::Response->new( 200, 'OK'); $response->header('Content-Type' => 'text/html'), $response->content("$cnt Working! (pid $pid)"); $cnt++; # print "Response:\n".Dumper($response); $c->send_response($response); } $c->close; undef($c); }