RMI Java服务器和客户端无法通信

时间:2016-04-21 18:59:44

标签: java rmi

RMIclient, 这是客户

package rmiclient;

import java.rmi.RMISecurityManager;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.*;

public class RMIClient {

 public static void main(String[] args) {
    RMIClient client = new RMIClient();
System.setSecurityManager(new RMISecurityManager());
    if(args.length == 0)
    {
    System.out.println("Please enter the correct server, exiting.");
    System.exit(0);
    }
    client.runMenu(args);
}//end Main()

public void runMenu(String[] args) {
    while (true) {
        int commandToRun = 0;
        int numberOfThreads;
        Scanner commandScanner = new Scanner(System.in);
        Scanner threadScanner = new Scanner(System.in);
    System.out.println();
        System.out.println("Enter the number for the command that you wish to execute.");
        System.out.println("1. Date & Time \n"
                + "2. Server Uptime \n"
                + "3. Memory Usage \n"
                + "4. Netstat \n"
                + "5. Current Users \n"
                + "6. Running processes \n"
                + "7. Exit");
        commandToRun = commandScanner.nextInt();
        System.out.println();
        if (commandToRun == 7) {
            System.out.println("Exiting");
            System.exit(0);
        } else if(commandToRun > 7 || commandToRun < 1) {
            System.out.println("Enter a valid command");
        } 
       /* else {
    if(commandToRun == 1 || commandToRun == 4)
    {
            System.out.println("Enter number of threads to run");
            numberOfThreads = threadScanner.nextInt();
            clientThreads thread[] = new clientThreads[numberOfThreads];
            for (int i = 0; i < numberOfThreads; i++) {
                thread[i] = new clientThreads(args, commandToRun);
            }
            for (int i = 0; i < numberOfThreads; i++) {
                thread[i].start();
        try{
        thread[i].join();
        }
        catch(Exception e){}
    }
    }
    else{
    numberOfThreads = 1;
            clientThreads thread[] = new clientThreads[numberOfThreads];
            for (int i = 0; i < numberOfThreads; i++) {
                thread[i] = new clientThreads(args, commandToRun);
            }
            for (int i = 0; i < numberOfThreads; i++) {
                thread[i].start();
        try{
        thread[i].join();
        }
        catch(Exception e){}}
            }*/
        } 
    }
}


class clientThreads extends Thread {

int commandToRun;
String host;

public clientThreads(String[] args, int commandToRun) {
    this.commandToRun = commandToRun;
    this.host = args[0];
}

public void run() {
    String serverResponse = "";
    try {
        Registry registry = LocateRegistry.getRegistry(1225);
        RMIInterface stub = (RMIInterface)registry.lookup("runCommand");
        serverResponse = stub.runCommand(commandToRun);
    } catch (Exception e) {
        System.out.println(e);
    }
System.out.println();
    System.out.println(serverResponse);
}

}

RMIINTERFACE,界面

package rmiclient;
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface RMIInterface extends Remote {
String runCommand(int commandToRun) throws RemoteException;

}

RMIserver,这是服务器

package rmiserver;

import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
//import java.rmi.RMISecurityManager;
import java.io.*;


public class RMIServer implements RMIInterface {

public RMIServer() {}

public static void main(String[] args) {
  try {
        RMIServer obj = new RMIServer();
     //   System.setSecurityManager(new RMISecurityManager());
        RMIInterface stub = (RMIInterface) UnicastRemoteObject.exportObject(obj, 0);
        Registry registry = LocateRegistry.getRegistry(1225);
        registry.rebind("runCommand", stub);
        System.out.println("Server starting");
    } catch (Exception e) {
        System.out.println(e);
    }
}

public String runCommand(int commandToRun) throws RemoteException {
    Runtime runtime = null;
    Process process = null;
    String command = "";
    String output = "";
    String newLine = "";
    BufferedReader inp = null;

    try {
        if (commandToRun == 1) {
            System.out.println("Running command: Date and Time");
            runtime = Runtime.getRuntime();
            command = "date";
            process = runtime.exec(command);
            inp = new BufferedReader(new InputStreamReader(process.getInputStream()));
            output = inp.readLine();
        } else if (commandToRun == 2) {
            System.out.println("Running command: Uptime");
            runtime = Runtime.getRuntime();
            command = "uptime";
            process = runtime.exec(command);
            inp = new BufferedReader(new InputStreamReader(process.getInputStream()));
            output = inp.readLine();
        } else if (commandToRun == 3) {
            System.out.println("Running command: Memory Use");
            runtime = Runtime.getRuntime();
            command = "free";
            process = runtime.exec(command);
            inp = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while ((newLine = inp.readLine()) != null) {
                output = output + "\n" + newLine;
            }
        } else if (commandToRun == 4) {
            System.out.println("Running command: Netstat");
            runtime = Runtime.getRuntime();
            command = "netstat";
            process = runtime.exec(command);
            inp = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while ((newLine = inp.readLine()) != null) {
                output = output + "\n" + newLine;
            }
        } else if (commandToRun == 5) {
            System.out.println("Running command: Current Users");
            runtime = Runtime.getRuntime();
            command = "who";
            process = runtime.exec(command);
            inp = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while ((newLine = inp.readLine()) != null) {
                output = output + "\n" + newLine;
            }
        } else if (commandToRun == 6) {
            System.out.println("Running command: Running Processes");
            runtime = Runtime.getRuntime();
            command = "ps -e";
            process = runtime.exec(command);
            inp = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while ((newLine = inp.readLine()) != null) {
                output = output + "\n" + newLine;
            }
        }
    } catch (Exception e) {
        System.out.println(e);
    }
    return output;
}

}

请帮忙,我觉得我非常接近客户端和服务器。非常接近使用SSH安全shell连接它们。 在客户端和服务器开始通信之后,应该很容易完成此程序,完成后它将能够从客户端(数字1-10)获取输入,并根据该数字给出服务器的响应。

0 个答案:

没有答案