#!/usr/bin/perl use strict; use IO::Socket; use POSIX 'WNOHANG'; use constant PORT => 12000; my $quit = 0; $SIG{CHLD} = sub { while (waitpid(-1,WNOHANG)>0) { } }; my $lisen_socket = IO::Socket::INET->new( LocalPort => PORT, Proto => 'tcp', Listen => 1, Reuse => 1 ); die "cant create a soocket \n" unless $lisen_socket; warn "server ready.. waiting for connection in port : 12000......\n"; while (!$quit) { my $connection; next unless $connection = $lisen_socket->accept; defined(my $child = fork()) or die "cannot fork : $!\n"; if($child == 0) { print "Inside child\n"; $lisen_socket->close(); &interact($connection); exit 0; print "exiting from child\n"; } $connection->close(); } sub interact { my $data; my $count = 0; my $sock = shift; #STDIN->fdopen($sock,"<") or die "cannot reopen STDIN : $!\n"; #STDOUT->fdopen($sock,">") or die "cannot reopen STDOUT : $!\n"; #STDERR->fdopen($sock,">") or die "cannot reopen STDERR : $!\n"; $| =1; while(defined $sock) { print $count; $data = <$sock>; if($data ne "") { print "Server : $data\n"; print $sock "type next line\n"; $count++; } } }