in reply to Backgrounding (daemonizing?) a Net::server

Thinking more about this, I realize that all you need to do is open the socket you are going to listen on before you fork. That way the client will alway find it ready to connect to even if the rest of the initialization is not done yet.

The following code almost works. See the note at the end to get it to really work.

#!perl -w use IO::Socket::INET; use Getopt::Std; use Proc::Daemon; use strict; my %opt; getopts("p:", \%opt); my $port = $opt{p} || 1923; my $listensock = IO::Socket::INET->new(LocalPort => $port, Listen => 5 +) or die "Socket: $!\n"; print STDERR "Process listening on port $port\n"; Proc::Daemon::Init(); # WARNING SEE NOTE while (my $client = $listensock->accept) { print $client "The time is ", scalar(localtime), "\r\n"; close($client); } # NOTE: This does not work because Proc::Daemon::Init closes ALL file # descriptors # If you delete this line from Daemon.pm # foreach $i (0 .. OpenMax) { POSIX::close($i); } # then it will work. Or you can just create your own function # which does everything that Init() does except that line