#!/usr/bin/perl -w use strict; use IO::Socket; use Sys::Hostname; use POSIX qw(:sys_wait_h); sub REAP { 1 until (-1 == waitpid(-1, WNOHANG)); $SIG{CHLD} = \&REAP; } $SIG{CHLD} = \&REAP; my $sock = new IO::Socket::INET( LocalHost => '127.0.0.1', LocalPort => 9898, Proto => 'tcp', Listen => 10, Reuse => 1); $sock or die "no socket :$!"; STDOUT->autoflush(1); STDERR->autoflush(1); my($new_sock, $buf, $kid); print STDERR "Parent $$: Server up\n"; while ($new_sock = $sock->accept()) { next if $kid = fork; die "fork: $!" unless defined $kid; # child now... print STDERR "Child $$\n"; # close the passive socket - not needed $sock->shutdown(2); # read from client $buf = <$new_sock>; $new_sock->shutdown(0); chomp $buf; print "Child $$: Read from client: $buf\n"; print $new_sock "READY\n"; print "Child $$: Sent READY, closing\n"; $new_sock->shutdown(2); exit; } continue { # parent closes the client since # it is not needed print STDERR "Parent $$: Closing child socket\n"; close $new_sock; } print STDERR "Parent $$: Should never get here\n";