I'm using this code as a part of a simple p2p application I'm working on, but I thought someone else might find it useful for something, or entertaining.
#!/usr/bin/perl -wuse strict;
use IO::Socket;
sub Wait {
wait;#wait needed to keep <defunct> pids from building up
}
$SIG{CHLD} = \&Wait;
my $server = IO::Socket::INET->new(LocalPort => 6800,
Type => SOCK_STREAM,
Reuse => 1,
Listen => 10) or die "$@\n";
my $client ;
while ( $client = $server->accept())
{
next if my $pid = fork;
die "fork - $!\n" unless defined $pid;
##Grab the handoff from client if you want
# my $client_info;
#
# while(<$client>)
# {
# last if /^\r\n$/;
# $client_info .= $_;
# }
###Hold client info for something useful
##$client_stuff;
select $client;
$¦ = 1;
print $client "HTTP/1.0 200 OK\r\n";
print $client "Content-type: text/html\r\n\r\n";
print $client '<H1>Hello World(!), from a perl web server</H1>';
close($client);
exit( fork );
}
continue
{
close($client); #kills hangs
kill CHLD => -$$;
}