
// usage:  java SvrString

// like Svr.java, but using strings

// accepts a command w or ps from the client, runs the command on the
// local machine, and then sends the command's output back to the
// client, which displays it there

// note:  at the bottom level, there are still the standard system
// calls, e.g. socket(), connect(), bind(), etc.; the difference is that
// in Java they are made by the underlying JVM, rather than by the
// programmer him/herself

import java.lang.*;
import java.io.*;
import java.net.*;

public class SvrString  {

   private static final int port=3000;

   public static void main(String[] args) {

      System.out.println("server is up ");
      // have special class for server sockets, which includes bind
      ServerSocket serversocket = null;
      try  {
         serversocket = new ServerSocket(port); 
      }
      catch(Exception e) {
         System.out.println( "couldn't create server socket; " + e );
         System.exit(1);    
      }
      try  {   
         while (true)  {
            System.out.println("waiting for client connection");
            try  { 
               Socket currentsocket = serversocket.accept();
               DataInputStream input = 
                  new DataInputStream(currentsocket.getInputStream());
               DataOutputStream output = 
                  new DataOutputStream(currentsocket.getOutputStream());
               System.out.println("connection from client" );

               // read w or ps command from the client; see Clnt.java
	       // comments regarding UTF
               String cmd = new String("");
               cmd = input.readUTF(); 
               System.out.println("executing   : "+ cmd);

               if (cmd.equals("w") || cmd.equals("ps"))  {
                  String cmdout = new String("");
                  try  {
                     // sets up a non-Java process running cmd on the
                     // native machine (i.e. not on the JVM); exec()
                     // throws IOException
                     Process process = Runtime.getRuntime().exec(cmd);
                     DataInputStream in = 
                         new DataInputStream(process.getInputStream());
                     // we won't have enough data to bother with
                     // buffering, but anyway we need it for readLine()
                     BufferedReader inb =
                         new BufferedReader(new InputStreamReader(in));
                     while((cmdout = inb.readLine())!= null)  {
                         output.writeUTF(cmdout);
                     }
                  }
   	          catch(IOException e)  {
                     System.out.println("" + e);
                  }
               }
   	       else output.writeUTF("invalid command: "+ cmd);	
	       // a homemade EOF
   	       output.writeUTF("end");
            }
            catch (Exception e)  {
               System.out.println("connection to current client broken");
            }
         }
      }
      catch (Exception e)  {
         System.out.println("fatal server error: " + e);
      }
   }
}


