#! perl -slw use strict; use threads; use threads::shared; use IO::Socket::INET; async { while( 1 ) { my $socket = IO::Socket::INET->new( PeerAddr => '127.0.0.1:54321', ) or warn 'No $socket: ', $! and sleep 1 and next; $socket->recv( my $message, 4096 ); Win32::Sleep( ( 100, 200, 300, 400, 500 )[ rand 3 ] ); } sleep 1; }->detach for 1 .. 40; while( ) { chomp; if( m[^msg: \s* (.*) \s* $ ]x ) { my $socket = IO::Socket::INET->new( Proto => 'udp', PeerAddr => '127.0.0.1:12345', ReuseAddr => 1, ) or warn 'No socket: ', $! and next; $socket->send( $1 || 'A standard message' ); } elsif( m[^dump] ) { my $socket = IO::Socket::INET->new( PeerAddr => '127.0.0.1:54321', ) or warn 'No socket: ', $! and next; $socket->recv( my $message, 4096 ); print $message; } elsif( m[^listeners: \s* ( \d* ) ]x ) { async { while( 1 ) { my $socket = IO::Socket::INET->new( Proto => 'udp', PeerAddr => '127.0.0.1:54321', ) or warn 'No socket: ', $! and next; $socket->recv( my $message, 4096 ); Win32::Sleep( ( 100, 200, 300, 400, 500 )[ rand 3 ] ); } }->detach for 1 .. $1 || 10; } else { print "Unknown command '$_'\n"; print <<'HELP'; msg: message text listeners: nn dump HELP } }