#!/usr/bin/perl -w use strict; use IO::Socket; $| = 1; #get the port to bind to or default to 8000 my $port = $ARGV[0] || 8000; #ignore child processes to prevent zombies $SIG{CHLD} = 'IGNORE'; #create the listen socket my $listen_socket = IO::Socket::INET->new(LocalPort => $port, Listen => 10, Proto => 'tcp', Type => SOCK_STREAM, Reuse => 1); #make sure we are bound to the port die "Cant't create a listening socket:\n$@" unless $listen_socket; warn "Server ready. Waiting for connections ... \n"; if(!-e "$ENV{HOME}/.fifopipe"){system 'mkfifo ~/.fifopipe' || system 'mknod ~/.fifopipe p' || die $@;} #wait for connections at the accept call while (my $connection = $listen_socket->accept) { my ($pid, $line); # perform the fork or exit if ($pid = fork) { #i'm the parent! #who connected? warn localtime(time)." Connection recieved ... ",$connection->peerhost,"\n"; #close the connection, the parent has already passed it off to a child. $connection->close(); open(FIFO, "< $ENV{HOME}/.fifopipe") or die $@; while(){$_ =~ s/a-z/A-Z/i;print $_;} close(FIFO); } else { #i'm the child! #die in case of forking not possible die "Error while forking: $@" unless defined $pid; #close the child's listen socket, we dont need it. $listen_socket->close; open(FIFO, "> $ENV{HOME}/.fifopipe") or die $@; while(<$connection>){print FIFO $_;}; # print FIFO "mouh\n"; close(FIFO); #if the child returns, then just exit; exit; } #go back and listen for the next connection! }