#!/bin/perl
#Output one line to the echo server at port 7 of labinfu.unipv.it
#(193.204.35.58).  Then input the line back from the server.

use Socket;
use FileHandle;	#for autoflush
use POSIX;      #for WIFEXITED and WEXITSTATUS

if (@ARGV == 1) {   #This @ARGV is in a scalar context
	$port = getservbyname('telnet', 'tcp') #look up telnet in /etc/services
		or die "$0: couldn't find port number for telnet";
} elsif (@ARGV == 2) {
	$port = $ARGV[1];
} else {
	die "$0: usage: hostname [port]";
}

socket(S, AF_INET, SOCK_STREAM, 0) or die "$0: $!";
S->autoflush();      #pp. 130, 444 in O'Reilly Perl book

$ip = inet_aton($ARGV[0])          or die "$0: inet_aton failed";
$address = sockaddr_in($port, $ip) or die "$0: sockaddr_in failed";

$dotted = inet_ntoa($ip);
print "Trying $dotted...\n";
connect(S, $address)               or die "$0: $!";
$name = gethostbyaddr($ip, AF_INET) or $name = $dotted;
print "Connected to $name.\n";

$pid = fork();
defined $pid or die "$0: $!";

if ($pid == 0) {
	#Arrive here if I am the child.
	#Copy from the server to the stdout.

	while (($_ = <S>) ne '' ) {  #Read one line from the socket
		print STDOUT $_;     #and write it to the stdout.
	}

	exit 0;
}

#Arrive here if I am the parent.
#Copy from the stdin to the server.

while (($_ = <STDIN>) ne '') {       #Read one line from the stdin
	print S $_;                  #and write it to the server.
}

shutdown(S, SHUT_RDWR)             or die "$0: $!";
print "Connection closed.\n";

$pid = wait();
if ($pid < 0) {
	die "$0: $!";
}

if (!WIFEXITED($?) || WEXITSTATUS($?) != 0) {
	print STDERR "child did not return exit status 0.\n";
}
exit 0;
