use strict; use IO::Socket; use threads; use threads::shared; my $numClients = shift; my %thread_status : shared; my $server_status : shared = 'threading'; $| = 1; my $screen_output : shared = 1; my $HOST = 'cdt-2x4w227x'; my $PORT = 7001; # Launch clients in their own thread print "Creating $numClients clients\n"; for (my $i=0; $i<$numClients; $i++) { my ($retries,$socket); $socket = IO::Socket::INET->new("$HOST:$PORT") || print "Socket creation error: $!\n"; while (!$socket) { print "Retrying socket connection for client $i\n"; $retries++; if ($retries > 5) { print "Exceeded retries while launching client $i\n"; last; } sleep 1; $socket = IO::Socket::INET->new("$HOST:$PORT") || print "Socket creation error on retry $retries: $!\n"; } if ($socket) { my $t = threads->new(\&handle_client,$socket,$i)->detach } else { print " Error creating socket: $i\n" } print "."; } print "Done creating clients\n"; sleep 1; $server_status = 'ready'; print "Waiting for client threads to finish\n"; while (my @keys = keys %thread_status) { my $count = @keys; print &time_stamp," Waiting for [$count] threads from [@keys]\n"; sleep 1; } sleep 1; #<------------------------------------------------------------ handle_client --> sub handle_client { my ($socket,$clientNum) = @_; my $line; my $tid = threads->self->tid; my $timeout = 2; $thread_status{$clientNum} = $clientNum; while ($server_status eq 'threading') { sleep 1 } #VINI print " $clientNum -> VINI\n" unless (!$screen_output); if ($socket) { $socket->send("VINI\n") || $socket->close && undef $socket && return } ^^^^^^ FAILS ON SEND ^^^^^^^ $line = <$socket> if ($socket); print " $clientNum <- $line" unless (!$screen_output); sleep rand($timeout); lock %thread_status; delete $thread_status{$clientNum}; $socket->close; threads->self->yield; return; }