#!/usr/bin/perl
# File: client

use Socket;   # load Socket module
($them,$port) = @ARGV;
$port = 6002 unless $port;
$them = 'localhost' unless $them;


$SIG{'INT'} = 'dokill';
sub dokill { kill 9,$child if $child }

$sockaddr = 'S n a4 x8';

chomp($hostname = `/usr/ucb/hostname`);

($name,$aliases,$type,$len,$thataddr) = gethostbyname($them);

$that = pack($sockaddr, &AF_INET, $port, $thataddr);

# Make the socket filehandle.
if (socket(S, &PF_INET, &SOCK_STREAM, 0)) { 
    print "socket ok\n";
}
else {
    die $!;
}

# Call up the server.

if (connect(S,$that)) {
    print "connect ok\n";
}
else {
    die $!;
}

# Set socket to be command buffered.

select(S); $| = 1; select(STDOUT);

# Avoid deadlock by forking.

if($child = fork) {
    while (<STDIN>) {
	print S;
    }
    sleep 1;
    do dokill();
}
else {
    while(<S>) {
	print;
    }
}
