tags:

views:

885

answers:

6

I wish to create a TCP server daemon process in Perl.

Which is the best framework/module for it?.

Is there anything that comes bundled with Perl?

Edit: Something that has start | stop | restart options would be great.

Edit: It has to be a Multi threaded server.

+4  A: 

Well - it's better if you could state what this daemon is supposed to do. As there are specialized frameworks/libraries for various tasks.

For simplest daemon that does nothing, just exists, you can easily do this:

#!/usr/bin/perl
use strict;
use warnings;
use Carp;
use POSIX qw( setsid );

daemonize();

do_your_daemon_stuff();

exit;

sub daemonize {
    chdir '/'                 or croak "Can't chdir to /: $!";
    open STDIN, '/dev/null'   or croak "Can't read /dev/null: $!";
    open STDOUT, '>/dev/null' or croak "Can't write to /dev/null: $!";
    defined(my $pid = fork)   or croak "Can't fork: $!";
    exit if $pid;
    setsid                    or croak "Can't start a new session: $!";
    open STDERR, '>&STDOUT'   or croak "Can't dup stdout: $!";
}

sub daemonize() was liften from perldoc perlipc (with minor change).

That's all - the code now properly daemonizes, and can do anything you want.

I just read your edit, that you want TCP server.

OK. Here is simplistic code:

#!/usr/bin/perl
use strict;
use warnings;
use Carp;
use POSIX qw( setsid );
use IO::Socket;

my $server_port = get_server_port();

daemonize();

handle_connections( $server_port );

exit;

sub daemonize {
    chdir '/'                 or croak "Can't chdir to /: $!";
    open STDIN, '/dev/null'   or croak "Can't read /dev/null: $!";
    open STDOUT, '>/dev/null' or croak "Can't write to /dev/null: $!";
    defined(my $pid = fork)   or croak "Can't fork: $!";
    exit if $pid;
    setsid                    or croak "Can't start a new session: $!";
    open STDERR, '>&STDOUT'   or croak "Can't dup stdout: $!";
}

sub get_server_port {
    my $server = IO::Socket::INET->new(
        'Proto'     => 'tcp',
        'LocalPort' => 31236,
        'Listen'    => SOMAXCONN,
        'Reuse'     => 1,
    );
    die "can't setup server" unless $server;

    return $server;
}

sub handle_connections {
    my $port = shift;
    my $handled = 0;

    while ( my $client = $port->accept() ) {
        $handled++;
        print $client "Hi, you're client #$handled\n";
        chomp ( my $input = <$client> );
        my $output = reverse $input;
        print $client $output, "\n";
        print $client "Bye, bye.\n";
        close $client;
    }

    return;
}

Just remember that this is blocking tcp server, so it will be able to handle 1 connection at the time. If you want more than 1 - it becomes more complex, and you have to ask yourself if you prefer multithreading (or multi-processing), or you prefer single-process, event based server.

depesz
I m writing a TCP server daemon.
someguy
thanx for the reply. But yes I do need to make it a multi threaded server.
someguy
So, first - restate your question. Sorry, but I will not guess what you need. Think about what you need, and *try* to write it. I will not write your code for you.
depesz
@depesz : i never asked for a sample code , but thanks anyways.I was looking for a existing perl module which can do this perferbly with threads.Reason for asking this questions is that , I searched cpan and found many implemenations , I did not know which is the best.
someguy
You started with question for "daemon". Then it was "tcp server, daemonized". Now it is "multi-threaded tcp server, daemonized". In couple of minutes, it will be "multi-threaded, tcp-based, chat server, daemonized". Ask your question as specific as possible. Not ask us to guess what you really need.
depesz
OK sorry my mistake.. i withdraw the question..u dont have to look at this question again..please ignore..sorry for taking your time..
someguy
+1  A: 

I've never had occasion to try it myself, but I believe POE is highly regarded for that sort of thing.

Here are some examples of TCP servers written with POE.

cjm
Thankyou i was looking for something like this..
someguy
A: 

"Something that has start | stop | restart options would be great"

There are modules on CPAN which will provide this. E.g., I can see Daemon::Generic which claims to be a framework to provide start/stop/reload for a daemon.

/I3az/

draegtun
A: 

You do not actually want to write multithreaded perl. Perl threads are broken - they do not work properly (in my opinion).

Creating a new perl thread clones the entire interpreter including all the data currently in scope - therefore is basically WORSE than creating a new process (which of course, would use copy-on-write) and less useful.

So you definitely don't want it multithreaded.

MarkR
so what you suggest is that writing a multi threaded tcp server in perl is a bad idea?But i read that perl thread are fine on v 5.8.
someguy
Perl threads are fine with the caveat that they use up more memory and take longer to start up than starting up a whole separate process; they can't share variables easily, the locking is yuck. Basically they were badly designed, which is not fixable without breaking compatibility with any program which uses them.
MarkR
+1  A: 

If possible I would consider looking at something like AnyEvent as an alternative to a pure threaded approach.

/I3az/

draegtun
A: 

A quick search reveals quite a few possibilities. Daemon::Generic seems to be straightforward to use.

In addition, there are many server modules for various protocols. In fact, HTTP::Daemon has been a core module for a while now.

Sinan Ünür