Java RMI
modify “MyClient.java”, “Method.java” and “MethodRemote.java”
After your modification, re-compile, and run server and client programs, the client asks the user to enter a string.
• If a string “time” is entered, the client will get current date and time from server by remotely invocating the method “action()” in server machine.
• If another string other than “time” is entered, then the client will get the capitalized version of the string the user just entered by remotely invocating the method “action()” in server machine.
• Your client program should keep asking the user’s input for a string unless an empty string is entered (i.e., just press ENTER key without anything else). When an empty string is entered, this client will quit.
while a client is being connected to the server, the server should be able to receive new clients’ connections for current date/time or capitalized strings. In other words, the server should concurrently handle multiple clients’ connections and requests well. And of course, after a client is closed, the server should keep running and be able to receive other clients’ connections, unless you manually shut down the server program.
• Note: You do NOT need to implement multi-threads to achieve that. Java RMI has already taken care of concurrent connections.
import java.rmi.*; import java.rmi.registry.*; import java.rmi.RemoteException; import java.rmi.server.*; public class MyServer{ public static void main(String args[]){ try{ MethodRemote stub = new MethodRemote(); Naming.rebind("rmi://35.39.165.77:5000/lab7", stub); //replace 35.39.165.77 with your server's IP address System.out.println("The Server is running!"); }catch(Exception e){System.out.println(e);} } }
import java.rmi.*; import java.io.*; public class MyClient{ public static void main(String args[]){ try{ Method stub=(Method)Naming.lookup("rmi://35.39.165.77:5000/lab7"); //replace 35.39.165.77 with your server's IP address System.out.println(stub.action(3,4)); }catch(Exception e){System.out.println(e);} } }
import java.rmi.*; import java.rmi.server.*; public interface Method extends Remote{ public int action(int x,int y)throws RemoteException; }
import java.rmi.*; import java.rmi.server.*; import java.util.Date; public class MethodRemote extends UnicastRemoteObject implements Method{ MethodRemote()throws RemoteException{ super(); } public int action(int x,int y){ return x+y; } }