#!/usr/bin/env perl use warnings; use strict; use POSIX qw(:sys_wait_h); use IO::Socket::INET(); use IO::Handle; sub reaper { 1 until waitpid(-1, WNOHANG) == -1; $SIG{CHLD} = \&reaper; } $SIG{CHLD} = \&reaper; my $server = IO::Socket::INET->new( Listen => 5, ReuseAddr => 1, LocalPort => 8888, Proto => 'tcp' ); while (1) { my $client; my $client_addr = accept($client, $server); next unless $client_addr; # Fork to handle the connection. my $pid = fork(); # Parent should just accept again. next if $pid; # Handling errors is good. die "fork failed: $!" unless defined $pid; # Child here. close($server); $client->autoflush(1); print "$$: client connected\n"; while (<$client>) { chomp; print "$$: got input: $_\n"; print $client "$_\n"; last if /^\s*quit\s*/; } print "$$: client disconnected\n"; close $client; exit; }