#! perl -slw use strict; use threads; use threads::shared; use IO::Socket; use constant CRLF => chr( 13 ) . chr( 10 ); my $server = IO::Socket::INET->new( LocalHost => 'localhost', LocalPort => '7070', Proto => 'tcp', Listen => 5, Reuse => 1, ) or die "Couldn't create listening socket"; my $running :shared = 0; $/ = $\ = CRLF; while( 1 ) { my $client = $server->accept; print "running: $running"; close( $client ), next if $running >= 5; print "Accepted connection from $client"; async { { lock $running; ++$running;} print "$client running"; while( ( my $input = <$client> ) ne 'quit' . CRLF ) { chomp $input; if( $input eq 'printhelp' ) { print $client 'Some help' . CRLF; } elsif( $input eq 'cmd1' ) { print $client 'CMD1 stuff' . CRLF; } elsif( $input eq 'cmd2' ) { print $client 'CMD2 stuff' . CRLF; } elsif( $input eq 'cmd3' ) { print $client 'CMD3 stuff' . CRLF; } else { print $client "Unrecognised command; '$input'" . CRLF; } } lock $running; --$running; print "$client done"; }->detach; } close $server;