RMI Java server and client will not communicate -
rmiclient, client
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 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 number command wish 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 valid command"); } /* else { if(commandtorun == 1 || commandtorun == 4) { system.out.println("enter number of threads run"); numberofthreads = threadscanner.nextint(); clientthreads thread[] = new clientthreads[numberofthreads]; (int = 0; < numberofthreads; i++) { thread[i] = new clientthreads(args, commandtorun); } (int = 0; < numberofthreads; i++) { thread[i].start(); try{ thread[i].join(); } catch(exception e){} } } else{ numberofthreads = 1; clientthreads thread[] = new clientthreads[numberofthreads]; (int = 0; < numberofthreads; i++) { thread[i] = new clientthreads(args, commandtorun); } (int = 0; < 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, interface
package rmiclient; import java.rmi.remote; import java.rmi.remoteexception; public interface rmiinterface extends remote { string runcommand(int commandtorun) throws remoteexception;
}
rmiserver, server
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 , 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; }
}
please help, feel i'm pretty close getting client , server. pretty close getting them connected utilizing ssh secure shell. after client , server start communicating should easy finish program, when finished able take input client (a number 1-10) , based off number give response server.
Comments
Post a Comment