From e5d41a1ea71d54d11cbb693f6ced5d0ae5d15fa1 Mon Sep 17 00:00:00 2001
From: dcw <d.white@imperial.ac.uk>
Date: Thu, 30 Nov 2017 17:15:02 +0000
Subject: [PATCH] realised that command-listener needs to take one line of
 input - additional command line arguments for the command to be run,
 similarly the command-client needs to pass that line..

---
 command-client   | 39 +++++++++++++++++++++++++++++++++++++++
 command-listener | 16 +++++++++++++---
 2 files changed, 52 insertions(+), 3 deletions(-)
 create mode 100755 command-client

diff --git a/command-client b/command-client
new file mode 100755
index 0000000..1e1264f
--- /dev/null
+++ b/command-client
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use Data::Dumper;
+use IO::Socket;
+
+my $host = shift @ARGV || "146.169.22.26";	# shell4..
+my $port = 55001;
+
+
+#
+#  my $socket = connect_server();
+#	connect to our server (trying several times if
+#	necessary), return the socket we receive.
+#	In case of repeated failure, return undef.
+#
+sub connect_server()
+{
+	warn "trying to connect to $host:$port\n";
+	foreach (1..5) {
+	    my $socket = new IO::Socket::INET('PeerAddr' => $host,
+				 'PeerPort' => $port,
+				 'Proto'    => 'tcp');
+	    return $socket if $socket;
+	    warn "trying again...\n";
+	    sleep 1;
+	}
+	return undef;
+}
+
+my $socket = connect_server();
+die "can't connect to $host:$port\n" unless $socket;
+warn "connected to $host..\n";
+my $line = join( " ", @ARGV );
+print $socket "$line\n";
+while( <$socket> )
+{
+	print;
+}
diff --git a/command-listener b/command-listener
index dc90ad2..7e293c0 100755
--- a/command-listener
+++ b/command-listener
@@ -1,7 +1,8 @@
 #!/usr/bin/perl
 #
 #	command-listener: daemon that listens for a connection on a
-#	port and runs a single specific command, sending the
+#	port, reads a single line of text (arguments) and runs a single
+#	specific command with those arguments, gathering and sending the
 #	results of running that command back to the client.
 #
 use strict;
@@ -18,7 +19,7 @@ use csglib::Log;
 my $logfile         = "/tmp/command-listener.log";
 
 # command to run..
-my $command	    = "/bin/date";		# example:-)
+my $command	    = "/bin/ls";		# example:-)
 
 my $port = shift @ARGV || 55001;
 my $quit = 0;
@@ -110,7 +111,16 @@ sub connection_loop ($$)
 #
 sub converse ()
 {
-	system( $command );
+	$log->warn( "reading line from client" );
+	my $args = <STDIN>;
+	unless( $args )
+	{
+		$log->warn( "converse: eof from client" );
+		print "client: gimme some args:-)\n";
+		return;
+	}
+	chomp $args;
+	system( "$command $args" );
 }
 
 
-- 
GitLab