#!/usr/bin/perl -w use strict; use IO::Socket::INET; my $server = IO::Socket::INET->new( LocalHost => 'localhost', LocalPort => 12345, Proto => 'tcp', Listen => SOMAXCONN, Reuse => 1 ) or die $!; print "server started\n"; $SIG{PIPE} = 'IGNORE'; while (1) { my $client = $server->accept(); print "accepted connection\n"; $client->autoflush(1); LOOP: for (1..1000) { my $count = int(rand 3)+1; # 1..3 32-bit BE uints, preceded by the respective count my $data = pack "N"x($count+1), $count, ($_)x$count; for (split //, $data) { last LOOP unless print $client $_; select undef, undef, undef, 0.01; # emulate slow delivery } } close $client; } #### #!/usr/bin/perl -wl use strict; use IO::Socket::INET; my $sock = IO::Socket::INET->new("localhost:12345") or die $!; my $buf; while ($sock->read($buf, 4)) { # get count my $count = unpack "N", $buf; $sock->read($buf, $count*4); # read actual data my @data = unpack "N$count", $buf; print "$count: @data"; } close $sock; #### $ ./888339_c.pl 3: 1 1 1 3: 2 2 2 1: 3 1: 4 1: 5 3: 6 6 6 2: 7 7 3: 8 8 8 2: 9 9 3: 10 10 10 3: 11 11 11 2: 12 12 3: 13 13 13 3: 14 14 14 1: 15 ...