#! perl -slw use strict; use Time::HiRes qw[ time usleep ]; use threads; use threads::shared; use IO::Socket; our $port //= 12345; my $svrN :shared = 0; my $clientN :shared = 0; my $is_winenv = ( $^O =~ /mswin|mingw|msys|cygwin/i ); my $start = time; async { my $svr = IO::Socket::INET->new( Listen => SOMAXCONN, Reuse =>1, LocalPort => $port, Timeout => 0.1, ) or die $!; while( my $client = $svr->accept ) { my $in = <$client>; print $client "echod:$in"; syswrite($client, '0') if $is_winenv; $client->shutdown( 2 ); close $client; ++$svrN; } }->detach; async { while( 1 ) { my $svr = IO::Socket::INET->new( PeerHost => 'localhost', PeerPort => $port, Reuse => 1, Timeout => 0.1, ) or usleep( 10_000 ), next; sleep 0; print $svr ++$clientN; my $echo = <$svr>; sleep 0; syswrite($svr, '0') if $is_winenv; $svr->shutdown( 2 ); close $svr; sleep 0; } }->detach; $|++; while( usleep 100_000 ) { printf "\rserver:$svrN client:$clientN cycles: %.3f/sec", $svrN / ( time() - $start ); }