#!/usr/bin/perl -w use threads ('yield', 'stack_size' => 64*4096, 'exit' => 'threads_only', 'stringify'); use IO::Socket::INET; my %shared_value :shared; sub sleeping { { lock %shared_value; #do somesing with %shared_value #... } sleep 60; } my $commandLinePrefix = "Command: "; sub listeningClient { my $client = shift; my $clientHost = $client->peerhost; $client->autoflush(1); print $client "Welcome to $0; type help for command list.\n$commandLinePrefix"; while ( <$client> ) { next unless /\S/; if ( /quit|exit|^q\n?\r?$/i ) { print $client "Closing connection\n"; last; } elsif ( /^sleep|^s\n?\r?$/i ) { threads->create( \&sleeping )->detach(); } else { print $client <new( Proto => "tcp", LocalAddr => $IP.':'.$PORT, Listen => SOMAXCONN, Reuse => 1); unless( $server ) { print "can't setup server. Error: $@\n"; exit; } print "[Server $0 accepting clients]\n"; while( my $connection = $server->accept() ) { my $thr = threads->create( \&listeningClient, $connection ); close $connection; } } sub main { my $ip = 'localhost'; my $port = 9000; use Getopt::Long; GetOptions( "ip=s" => \$ip, "port=i" => \$port ); startServer( $ip, $port ); } main();