#!/usr/bin/perl

use IO::Socket;

# usage: $0 <host> <ip>

$ip = shift || "127.0.0.1";
$port = shift || 4242;

$SIG{'QUIT'} = $SIG{'INT'} = $SIG{'TERM'} = $SIG{'HUP'} 
= sub { print "bang\n"; close $socket; exit 1 };

$SIG{'PIPE'} 
  = sub { print "pipe\n"; close $connection; close $server; exit 2 };

my $socket = new IO::Socket::INET(PeerAddr => $ip,
                                  PeerPort => $port,
                                  Proto    => 'tcp',
                                  Type     => SOCK_STREAM);

die "$!" unless $socket;

while (<$socket>) {

  /^(\d+) /;

  print "$1\n" unless $1 % 1000;
  sleep 10 unless $1 % 10000;
}

print "* all done\n";

close $socket;
