Program 4


In this program, you will create a simple server that will execute commmands on a remote host and return the results to the client. This is similar to the rsh command.

The client (named remote here) will execute commands of the form:
where host is the name of the host where the server is residing and command is the command to be executed. For example,
which would cause the server to execute ls and return the results. Note that this server is stateless, so it doesn't keep track of where it is or of any previous commands, so an ls is probably a silly thing to ask it to do. If the server were running as root, how would it know what directory to ls. One way is to have the client logon as a specific user and then keep track of the current directory.

However, you could ask it to copy a file to the client with:
The command can be executed with the system command, which is described in the man pages. There is a better method described below.

The next problem is redirecting the command output to the client. You need to change stdout from the terminal to some other place. Using a file or attempting to store the output in memory may be a problem because you could easily fill up the disk or memory. You might be able to get this to work, but a better solution is to use pipes and the popen command. A pipe is a communication stream between two processes, which you have probably used when you "pipe'd" output from one process to another, as in "ls | wc". Note that the popen command includes the system command execution.

/* ====================================================> popen_test.c
 *
 * Simple demonstration of the popen command that opens a pipe
 * for a process to use to communicate with a command that is specified.
 *
 * =====================================================================
 */

#include 

void main ()
{
   char   buf [80], nbyte;
   FILE   *pf;


   /*
    * Issue an ls command and specify a desire to read the output
    */

   pf = popen ("ls", "r");


   /*
    * Read and write the results.
    */

   while (1)
   {
      if (fscanf (pf, "%s", buf) <= 0)
         break;
      printf ("%s\n", buf);
   }

   pclose (pf);
}

Instead of writing the data to the terminal, you would want to return it to the client.

Using UDP to handle the communication between the client and server, build a system that will implement the remote command. You should expect the command to come in as a null-terminated string, and remember to handle error conditions.

Turn in a script showing your client and server running and executing some simple commands, such as ls, cat'ting a file, cp, rm or whatever you choose. Since you don't have full duplex operation at this time, don't try vi or anything that requires additional data from the client to terminate. If you allow full duplex operation, you have essentially created a telnet type of capability.