#!/usr/bin/perl -- use strict; use warnings; use threads stack_size => 4096; use IO::Socket::INET; our $host = "localhost:".int(rand 99999); async( sub { my $tid = threads->tid; use Time::HiRes qw/ usleep /; my $server = IO::Socket::INET->new( Proto => "tcp", LocalHost => $host, Listen => 1, Reuse => 1, ) or die "Cannot start tcp server: $!\n $@\n "; print "listening on $host\n"; sleep 1; my $client = $server->accept(); while(<$client>){ print "reader($tid) $_"; last if /exit/i; } }); async( sub { my $tid = threads->tid; use Time::HiRes qw/ usleep /; my $socket = IO::Socket::INET->new( Proto => "tcp", PeerHost => $host, ) or die "Cannot start client : $!\n $@\n "; for(1..20){ usleep 2000*rand(500); # 2000 sleep for 2 millisecond $socket->print( "writer($tid) ".scalar(gmtime)."\n" ); } $socket->print( "writer($tid) ".scalar(gmtime)." exit\n" ); }); $_->join for threads->list; __END__