Are you really looking for a multithreaded daemon (i.e. one that uses Perl threads) or a multi-process daemon? Here's a (very simple) example that does what you describe with Net::Server::PreFork:

#!/usr/bin/perl package MyEchoServer; use strict; use warnings; use Net::Server::PreFork; use base("Net::Server::PreFork"); my $self = bless { server => { port => [ 1025 ] }, logfile => "/tmp/clog", },"MyEchoServer"; open(my $clog,">>",$self->{logfile}) or die "Can't open $self->{logfil +e} : $!"; my %accept = ( ipconfig => "10.10.0.0", set => sub { $self->{value}->{$_[0]}=$_[1] }, get => sub { print "$_[0] is set to $self->{valu +e}->{$_[0]}\n" }, ); $self->run(); sub process_request { my $self = shift; print "> "; while (<STDIN>) { chomp; my ($command,@param) = split; if (exists($accept{$command})) { print {$clog} "$command ".join(" ",@param)."\n" or die "Can't print to $self->{logfile}"; if (ref($accept{$command}) eq "CODE") { $accept{$command}->(@param); } else { print $accept{$command}."\n"; } } else { print "Don't know command $command\n"; } print "> "; } }

This is the interaction with the server (using socat instead of netcat):

sh-2.05b$ socat readline tcp4:localhost:1025 > unknown Don't know command unknown > ipconfig 10.10.0.0 > set var1 one > get var1 var1 is set to one

And this is the STDERR of the server and content of /tmp/clog:

2006/01/05-10:11:06 MyEchoServer (type Net::Server::PreFork) starting! + pid(23443) Binding to TCP port 1025 on host * Group Not Defined. Defaulting to EGID '104 111 104 29' User Not Defined. Defaulting to EUID '1000' 2006/01/05-10:11:24 Server closing! sh-2.05b$ cat /tmp/clog ipconfig set value one get value

A couple of caveats with this code:

Hope this helps.

A computer is a state machine. Threads are for people who can't program state machines. -- Alan Cox

In reply to Re: Multi-threaded daemon by tirwhan
in thread Multi-threaded daemon by carric

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.