Dr. Mu has asked for the wisdom of the Perl Monks concerning the following question:
I'm writing a small server using HTTP::Daemon and threads in ActivePerl 5.8.4 under WinXP. Below are the routines used to accept and service connections:
My problem is that defunct connections never seem to close, leaving the threads that service them active. If I remove the line,sub RunServer { my $connectno = 0; while (1) { my $daemon; my $port = $Admin{port}; until ( $daemon = HTTP::Daemon->new( LocalAddr => "", LocalPort => $port, Listen => 5 ) ) {sleep 2} my $readable = IO::Select->new; $readable->add($daemon); print "Listening...\n"; while (1) { while (IO::Select->select($readable, undef, undef, 0)) { my $connex = $daemon->accept; threads->create(\&Handler, $connex, ++$connectno)->detach } last if $port != $Admin{port}; sleep .1 } } } sub Handler { my ($connex, $connectno) = @_; my $peeraddr = $connex->peeraddr; $peeraddr = join('.', unpack('C4', $peeraddr)); print "Connection $connectno at " . localtime(time) . " from $peerad +dr\n"; while (my $req = $connex->get_request) { my $req_uri = $req->uri; my $req_content = $req->content; my $req_method = $req->method; print " Connection: $connectno Request: $req_method $req_content +$req_uri\n"; if ($req_method eq 'POST') { DoPost($connex, $peeraddr, $req_content, $req_uri) } elsif ($req_method eq 'GET') { DoGet($connex, $peeraddr, $req_uri) } else { SendNotFound($connex) } } print "Connection $connectno finished.\n"; sleep 1 while ($connex->connected); $connex->close; print "Connection $connectno closed.\n\n"; }
forcing the close and returning from the thread, perl.exe crashes after about five or six connections.sleep 1 while ($connex->connected);
Perhaps someone more experienced with these modules can shed some light...
Thanks,
Phil
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Closing Connections using HTTP::Daemon
by InfiniteSilence (Curate) on Nov 11, 2005 at 22:22 UTC | |
by Dr. Mu (Hermit) on Nov 11, 2005 at 23:28 UTC | |
by Dr. Mu (Hermit) on Nov 11, 2005 at 23:41 UTC |