To get the code to work reliably on Windows7 (ActiveState Perl v5.12.4) I had to add a sleep of at least 15ms after the thread creation. I use 50ms for good measure:
use Time::HiRes qw( usleep ); ... while (my $c = $d->accept) { threads->create(\&process_one_req, $c)->detach(); usleep(50_000); }
Update: after attempting AJAX POSTs with some jQuery/JavaScript, the Perl web server code was not reliable. I seemed to loose AJAX messages. (As a workaround, I installed an AJAX error handler in the JavaScript code, which performs the same AJAX POST again (well, using setTimeout)... that worked, since I can tolerate multi-second delays -- the app updates graphs that are generated only every 4 seconds.)

Update: after playing around with this some more, I was able to get reliable operation, and handle multiple browsers and multiple requests per connection/browser if I closed the client connection/socket in the server, and the daemon/server socket in the client:

use HTTP::Daemon; use threads; my $d = HTTP::Daemon->new(LocalAddr => $ARGV[0], LocalPort => 80, Reuse => 1, Listen => 20) || die; print "Web Server started, server address: ", $d->sockhost(), ", serve +r port: ", $d->sockport(), "\n"; while (my $c = $d->accept) { threads->create(\&process_client_requests, $c)->detach; $c->close; # close client socket in server } sub process_client_requests { my $c = shift; $c->daemon->close; # close server socket in client while(my $r = $c->get_request) { if ($r->method eq "GET") { my $path = $r->url->path(); $c->send_file_response($path) or die $!; #or do whatever you want here } else { print "unknown method ".$r->method."\n" } } $c->close; }

In reply to Re^3: A simple web server with HTTP::Daemon and threads by mrajcok
in thread A simple web server with HTTP::Daemon and threads by pg

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.