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

I'm going to be sending a socket communications from one computer to another at specific times of the day (ie. 9am, 10am, etc..). I will use IO::Socket with some variation of the code below on the receiver end:
use IO::Socket; my $socket = new IO::Socket::INET ( LocalHost => 'host', LocalPort => 'portnumber', Proto => 'tcp', Listen => 1, ); die "No socket" unless $socket; my $connect= $sock->accept(); while($connect) { print $_; } close($socket);
As I understand it, the above code will connect to a socket and print out the contents of the what the sender is sending.

My question is: Do I run this script continously using cron or how do I insure that I accept the socket once the sender sends it? Let's say that the sender initiates his socket sometime between 9 and 9:01. Do I have to repeatedly run the above script between those times in order to insure that I receive the socket? Is there a way to have a background process running that would automatically run my script and then accept the socket?

Thanks.

Replies are listed 'Best First'.
Re: Using Sockets
by Zaxo (Archbishop) on Oct 28, 2002 at 01:05 UTC

    It is fairly simple to make your server run as a daemon. See Super Search on 'daemon' for examples. The basic steps are to fork, exit the parent, and in the child close unneeded file handles, become session leader with &POSIX::setsid, change directory to /, and adjust umask to zero.

    Such a daemon should select or use IO::Select to wait for its socket to become readable. The effect is to make the daemon sleep until there is work for it to do.

    After Compline,
    Zaxo

      If the AM is on *nix I would also recommend looking at xinetd (the replacement for inetd).

      If the service is going to be used lightly then this would be a simpler to impliment and not require the overhead a full fledged deamon does.



      grep
      Mynd you, mønk bites Kan be pretti nasti...
Re: Using Sockets
by fokat (Deacon) on Oct 28, 2002 at 05:57 UTC
    An easier way for your process to become a daemon, is to use Proc::Daemon.

    What I would do, is leave a server (as a daemon) running continuously (it would be blocked in an accept() most of the time). Then have the client "call home" when required, possibly through cron or similar.

    Best regards