TASdvlper has asked for the wisdom of the Perl Monks concerning the following question:
use strict; use warnings; use IO::Socket; use POSIX qw(:sys_wait_h); # use diagnostics; my $local = "172.23.167.114"; my $port = "7070"; my $maxconn = SOMAXCONN; my ($new_sock, $c_addr, $buf, $pid, $status); $|++; # NOTE: use netstat to see network properties. # ignore child processes to prevent zombies # $SIG{CHLD} = 'IGNORE'; # Using this actually makes all return codes + = -1 !! # $SIG{PIPE} = 'IGNORE'; sub REAPER{ my $pid = wait; # while ( waitpid(-1,WNOHANG) > 0 ) { } $status = $? >> 8; $SIG{CHLD} = \&REAPER; # wait for next child process } $SIG{CHLD} = \&REAPER; # wait for 1st child process my $sock = new IO::Socket::INET ( LocalHost => $local, LocalPort => $port, Proto => 'tcp', Listen => $maxconn, Reuse => '1', ); die "Could not create socket: $@ \n" unless $sock; print "RECEIVER::IP = $local PORT = $port MaxConn = $maxconn\n"; while (($new_sock, $c_addr) = $sock->accept() ) { die "Can't fork: $!" unless defined ($pid = fork()); if ( $pid == 0 ) { # Child code my ($client_port, $c_ip) = sockaddr_in($c_addr); my $client_ipnum = inet_ntoa($c_ip); my $client_host = gethostbyaddr($c_ip, AF_INET); print "CHILD CODE [cpid::$$]: Connection from: $client_host [$cl +ient_ipnum ", $new_sock->peerport, "] \n\n"; while (defined($buf = <$new_sock>) ) { if ($buf =~ /^EXECUTE:(.*)/ ) { print $1, "\n"; system("/msg/spgear/tools/bin/perl $1") || die "Could not +run program: $1 -- $!\n"; if ( defined $status ) { print $new_sock "RETURN CODE: $status \n"; undef $status; } else { print $new_sock "RETURN CODE: undef \n"; } } else { print "INVALID SYNTAX\n"; print $new_sock "INVALID SYNTAX\n"; } } # exit(0); } else { # Parent code $new_sock->close; print "\nPARENT CODE [ppid::$$ cpid:$pid]\n"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: In Solaris accept() is not restarted [on Solaris] ...
by pg (Canon) on Oct 24, 2003 at 17:11 UTC | |
by TASdvlper (Monk) on Oct 24, 2003 at 22:06 UTC | |
|
Re: In Solaris accept() is not restarted [on Solaris] ...
by iburrell (Chaplain) on Oct 24, 2003 at 20:55 UTC | |
|
Re: In Solaris accept() is not restarted [on Solaris] ...
by Anonymous Monk on Oct 24, 2003 at 17:10 UTC |