Your question seems rather strange to me because this would be rather unusual. If you are building a server, then normal would be to have a single "listen or passive" socket. The server listens on that single socket and dispatches children as the requests come in (the server code itself is short and to the point). You can tell the OS that you allow multiple requests to be pending via the listen=>10 parameter when you create the listen (passive) socket.
Once you accept the connection from the "listen socket", it becomes an "active socket" and the server keeps "listening".
Here is the basic "server framework" for a simple application.
#!/usr/bin/perl -w
use strict;
use IO::Socket;
use POSIX ":sys_wait_h";
my $RW_BUF_LEN = 256;
my $SERVER_TIMEOUT_SECS = 20;
my $active;
# note SOMAXCONN is system max of queue for incoming clients
# Listen=>1 in this app would have also have been just fine
$SIG{PIPE} = sub {close $active; exit (3)};
$SIG{ALRM} = sub {close $active; exit (7)};
$SIG{CHLD} = sub {local ($!, $?);
while (waitpid(-1, WNOHANG) > 0){} };
my $passive = IO::Socket::INET->new( Proto => 'tcp',
LocalPort => 12455,
Listen => SOMAXCONN,
Reuse => 1,
Type => SOCK_STREAM
)
or die "Server is unable to start: $!\n";
while(1)
{
$active = $passive->accept() or next; #blocking "wait"
# next not really necessary
# Perl > 5.8 has deferred signals by default on
# so a signal cannot occur within accept()!
# but safety is a good thing!
die "Bad Fork! $!\n" if ( !defined(my $pid = fork()) );
if ($pid != 0) # Parent, Note: Windows has a negative $pid
# (fork emulation)
{
close $active; #parents do not talk to anybody
next;
}
####### we are the child #########
close $passive; #Client's don't listen for connections!
#### blah whatever the child does.....
## it "talks" on the $active socket's filehandle
}
|