#! perl -slw use strict; use threads; use IO::Socket; use Storable qw[ freeze thaw ]; use Data::Dump qw[ pp ]; $|++; my %hash; @hash{ 'a'..'h' } = 1 .. 8; my $lsn = new IO::Socket::INET( Listen => 5, LocalPort => '12345' ) or die "Failed to open listening port: $!\n"; async{ print 'accept loop started'; while( my $c = $lsn->accept ) { print "Connect from $c"; binmode $c; async { while( my $cmd = <$c> ) { print "Got '$cmd'"; last if $cmd =~ m[^quit]; printf $c "%s", pack "N/a*", freeze \%hash; } print "Got quit from $c"; close $c; }->detach; } }->detach; sleep 2; for my $client ( 1 .. 2 ) { # async { print "Client $client started"; my $s = new IO::Socket::INET( 'localhost:12345' ) or die "Failed to connect to server: $!"; print "client $client connected"; binmode $s; for ( 1 .. 2 ) { print $s 'givemeit'; print 'Sent givemeit'; my $len; read( $s, $len, 4 ) or die "Read failed: $!"; $len = unpack 'N', $len; print "read length: $len"; my $hashStr; read( $s, $hashStr, $len ) or die "Read failed: $!"; print "Read hashstr length: ", length $hashStr; my %hash = %{ thaw $hashStr }; pp \%hash; } print $s 'quit'; close $s; print "client; $client disconnected"; # }->detach; } sleep 1 while 1;