Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

HTTP::Daemon Script.

by Anonymous Monk
on Jul 06, 2001 at 09:57 UTC ( [id://94385]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

in this code, the daemon work in with only one client. how can i make daemon working with many clients *at once*?
use HTTP::Daemon; my $d = HTTP::Daemon->new(LocalPort => 8080); while (my $c = $d->accept) { while (my $r = $c->get_request) { print $r->method ."\n" . $r->url->path ."\n"; } $c->close; undef($c); }

Replies are listed 'Best First'.
Re: HTTP::Daemon Script.
by physi (Friar) on Jul 06, 2001 at 13:02 UTC
    Don'tr know anything about the HTTP::Daemon module, but you have to use fork to get a new process for the communication with the client. Then the main process can accept mor connetions at the same time. Try:
    use HTTP::Daemon; my $d = HTTP::Daemon->new(LocalPort => 8080); while (my $c = $d->accept) { my $pid= fork; if (! $pid) { ###It's the child process here while (my $r = $c->get_request) { print $r->method ."\n" . $r->url->path ."\n"; } $c->close; undef($c); } ### And that's the end of the while loop from the father process }
    If you cannot fork on your system (i.e. Perl4Win 5.003) you have to use select and/or Multiplex to do the job. But the easiest way might be to fork your processes.
    ----------------------------------- --the good, the bad and the physi-- -----------------------------------
      You don't have to close and undef $c while you're the parent, after the fork?

      --
      $Stalag99{"URL"}="http://stalag99.net";

Re: HTTP::Daemon Script.
by MZSanford (Curate) on Jul 06, 2001 at 13:55 UTC
    Forking is probably the best solution, but, as always, there is more that one way to do it. For a rather exhaustive list of the possibilities i would suggest Network Programming With Perl. The book covers using
    - fork()
    - Threading (not for production systems)
    - IO Multiplexing (complex, but works on non-fork systems)
    - Pre-forking/pre-threading
    - And a ton more information on style, security, and production ready code.

    A quick answer is the fork() solution above. But if you are going to be doing alot of traffic, i suggest giving the book a read.
    may the foo be with you

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://94385]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-04-25 11:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found