#!/usr/bin/perl # for debugging, use .../perl -d ############################################################################## # # TCP/IP client: # - Connect to a server # - Allow user to type line(s) of input # - Send line to server # - Displays whatever the server sends back # # Usage: TCPclient serverName portNumber # ############################################################################## use Socket; # import Socket.pm $serverName = $ARGV[0] || "localhost"; # if none use 'localhost' $serverPort = $ARGV[1] || 8467; # if none use port 8467 $SIG{'TERM'} = 'sigHandler'; $SIG{'CHILD'} = 'sigHandler'; sub sigHandler { local($signal) = @_; print STDERR ("Caught signal $signal - terminating process\n"); exit(1); } &setConnect(); # setup connection &forkChild(); # fork a child exit(0); ############################################################################## # Establish communication between client and server ############################################################################## sub setConnect { ### Client information ### my $clientName = `hostname`; chomp($clientName); my $clientAddrBin = gethostbyname($clientName); my @clientAddrNum = unpack("C4", $clientAddrBin); my $clientAddr = join('.', @clientAddrNum); # my $clientAddr = inet_ntoa($clientAddrBin); #same as prev 2 lines print "Client name: $clientName \n"; print "IP address : $clientAddr \n\n"; ### Server information ### my $serverAddrBin = gethostbyname($serverName); my @serverAddrNum = unpack("C4", $serverAddrBin); my $serverAddr = join('.', @serverAddrNum); # my $serverAddr = inet_ntoa($serverAddrBin); #same as prev 2 lines print "Server name: $serverName \n"; print "IP address : $serverAddr \n"; print "Port number: $serverPort \n\n"; print ("Trying to connect to $serverName.....\n"); my $protocol = getprotobyname("tcp"); my $serverSocket = pack_sockaddr_in($serverPort, $serverAddrBin); ### create socket filehandle ### socket(SOCKET, PF_INET, SOCK_STREAM, $protocol) || die "$0: Cannot create $protocol socket- $!"; ### connect socket to remote server ### connect(SOCKET, $serverSocket) || die "$0: Cannot connect to server at port $serverPort- $!"; print ("Connected to server at port $serverPort \n\n"); } ############################################################################## # Fork a new process to establish dual communication paths ############################################################################## sub forkChild { select SOCKET; $| = 1; # set SOCKET to unbuffered select STDOUT; $| = 1; # set STDOUT to unbuffered $childpid = fork(); # fork a new process (dupl this pgm) defined $childpid || die "$0: $!"; # childpid == null? then die &parent() if ($childpid != 0); # call parent is child pid not = 0 &child() if ($childpid == 0); # call child if child pid = 0 } ############################################################################## # I'm the parent, read from STDIN, and write into the socket ############################################################################## sub parent { while ($data = ) { # read from STDIN print SOCKET $data; # write to socket } ## Shutdown both ends (i.e. '2') of the socket ### shutdown(SOCKET, 2) || die "$0: Cannot shut down socket $!"; waitpid($childpid, 0); # collect the child } ############################################################################## # I'm the child, read from the socket, and write to STDOUT ############################################################################## sub child { while ($data = ) { # read from socket print STDOUT $data; # write to STDOUT } print STDERR "Connection closed by foreign host.\n"; exit (0); }