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
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.