my $REQUESTED_SESSIONS : shared = 0; my $ACT_SESSION_CNT : shared = 0; # ************** START ************** # print "[SERVER] START\n"; my $socket = create_socket($protocol, $port, $listen, $reuse); while ( ($client, my $client_addr) = $socket->accept()) { if($REQUESTED_SESSIONS == 0) { print "[SERVER] ------- Sessions requested..? -------\n"; # get the desired number of sessions from client $REQUESTED_SESSIONS = receive_command($client); if(!defined $REQUESTED_SESSIONS) { $REQUESTED_SESSIONS = 10; } $ACT_SESSION_CNT = 0; next; } # get CLIENT data my ($client_port, $client_ip) = sockaddr_in($client_addr); my $client_ip_num = inet_ntoa($client_ip); print "[SERVER] received ([$client_ip_num] on $client_port)\n"; print "[threading]\n"; send_command($client, "connection_established"); my $thread = threads->new(\&process_session, $client)->detach; } # ************** END ************** # ##################################################################### ## ## function for processing client ## ##################################################################### sub process_session { my $client = shift; my $thread_id = threads->self()->tid(); print "[SERVER] [Thread $thread_id]\n"; increment_act_sessions(); # every thread waits for all connections while($ACT_SESSION_CNT < $REQUESTED_SESSIONS) { sleep 1; } # send a message to every client that the test is over send_command($client, "close_session"); print "[closing the client - THREAD $thread_id]\n"; close $client; lock($REQUESTED_SESSIONS); $REQUESTED_SESSIONS--; if($REQUESTED_SESSIONS == 0) { print "[SERVER] Finished.\n"; print "[SERVER] Ready for more!\n"; print "[SERVER] ------- Sessions requested..? -------\n"; } }