#!/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 }