#!/usr/bin/env perl use strict; use warnings 'all'; $|= 1; my $n= shift || 2; my $active= 0; sub getFromProducer { use IO::Socket::INET; my($socket, $data); $socket= new IO::Socket::INET ( PeerHost=> '127.0.0.1', PeerPort=> '5000', Proto=> 'tcp' ) or die "ERROR in Socket creation: $!\n"; $socket->autoflush(1); $data= <$socket>; chomp($data); print $socket "received\n"; $socket->close(); return $data; } print "Client started.\n"; while(my $data= &getFromProducer) { if($active<$n) { if(my $pid= fork) { $active++; } elsif(defined($pid)) { print ". Got $data.\n"; select(undef, undef, undef, int(rand()*10)+1); exit } else { die "Cannot fork: $!\n"; } } if($active==$n) { wait; $active--; } } print "Client finished.\n"; # vim: set nohls nowrap ts=2 sw=2 sts=2 et ft=perl: