190 likes | 295 Views
RMI Example. Compilation:. /home2/fccheng/temp/RMI2> javac MyObject.java /home2/fccheng/temp/RMI2> javac RemoteServer.java /home2/fccheng/temp/RMI2> javac RemoteInterface.java /home2/fccheng/temp/RMI2> javac RemoteClient1.java /home2/fccheng/temp/RMI2> javac RemoteClient2.java.
E N D
Compilation: /home2/fccheng/temp/RMI2> javac MyObject.java /home2/fccheng/temp/RMI2> javac RemoteServer.java /home2/fccheng/temp/RMI2> javac RemoteInterface.java /home2/fccheng/temp/RMI2> javac RemoteClient1.java /home2/fccheng/temp/RMI2> javac RemoteClient2.java
rmic RemoteServer -rw-r--r-- 1 fccheng staff 1712 May 12 17:40 RemoteServer_Skel.class -rw-r--r-- 1 fccheng staff 3223 May 12 17:40 RemoteServer_Stub.class
Java policy for RMI grant { permission java.net.SocketPermission "*:1024-65535", "connect,accept"; permission java.net.SocketPermission "*:80","connect"; };
start rmiregistry $ rmiregistery 1111 & Default TCP port: 1099
Run Server • Run server program: (see RunServer script) • java -Djava.rmi.server.codebase=http://140.129.20.87/~fccheng/ • temp/RMI2/ -Djava.rmi.server.hostname=140.129.20.87 • -Djava.security.policy=java.policy RemoteServer IP TCP-port
Run Clients • Run client program: (see RunClient1 and RunClient2) • java -Djava.rmi.server.codebase=http://140.129.20.110/~cheng/ • temp/RMI2/ • -Djava.security.policy=java.policy RemoteClient1 • ServerIP:TCP-Port
Running Example Host Information: Host IP JDK OS App. ====== ============ ======== ===== ============== alpha: 140.129.20.247 JDK 1.1.6 Sol2.5 (client only) gamma: 140.129.20.87 JDK 1.2 Sol2.6 (client and server) aimm: 140.129.20.110 JDK 1.2 Sol2.6 (client and server) ====== ============ ======== ===== ==============
Snapshot:Client site aimm ***************************************************** aimm:/export/home/cheng/temp/RMI2> RunClient1 140.129.20.87:1112 Running Client... Sending MyObject to Server ... The server says : Total Message: 1 Client 1 aimm:/export/home/cheng/temp/RMI2> RunClient2 140.129.20.87:1112 Running Client... Sending MyObject to Server ... The server says : Total Message: 2 Client 1 2nd Client aimm:/export/home/cheng/temp/RMI2> *****************************************************
Snapshot:Client site alpha ***************************************************** alpha:/home2/cheng/temp/RMI2> java RemoteClient1 140.129.20.87:1112 Running Client... Sending MyObject to Server ... The server says : Total Message: 3 Client 1 2nd Client Client 1 alpha:/home2/cheng/temp/RMI2> *****************************************************
Snapshot: Server gamma ***************************************************** gamma:/home2/fccheng/temp/RMI2> RunServer 140.129.20.87 1112 Running Server ... Server:rebining ... Server got[8]:Client 1 Server got[10]:2nd Client Server got[8]:Client 1 *****************************************************
/* MyObject.java */ public class MyObject implements java.io.Serializable { private int x; private String msg; public MyObject (String s) { msg = s; x = msg.length(); } public int lenMsg() { return x; } public String getMsg() { return msg; } public void setMsg(String s) { msg = s; } }
/* RemoteClient1.java */ import java.rmi.*; public class RemoteClient1 { public static void main(String args[]) { System.out.println("Running Client..."); System.setSecurityManager(new RMISecurityManager()); try { RemoteInterface server = (RemoteInterface) Naming.lookup("rmi://"+args[0] + "/"+"ObjectServerTest"); MyObject msgObj = new MyObject("Client 1"); System.out.println("Sending MyObject to Server ..."); MyObject retObj = server.msgsend(msgObj); System.out.println("The server says :\n" + retObj.retMsg()); } catch (Exception e){ System.out.println("Error while performing RMI"); } } }
/* RemoteClient2.java */ import java.rmi.*; public class RemoteClient2 { public static void main(String args[]) { System.out.println("Running Client..."); System.setSecurityManager(new RMISecurityManager()); try { RemoteInterface server = (RemoteInterface) Naming.lookup("rmi://"+args[0] + "/"+"ObjectServerTest"); MyObject msgObj = new MyObject(”2nd Client"); System.out.println("Sending MyObject to Server ..."); MyObject retObj = server.msgsend(msgObj); System.out.println("The server says :\n" + retObj.retMsg()); } catch (Exception e){ System.out.println("Error while performing RMI"); } } }
/* RemoteInterface.java */ public interface RemoteInterface extends java.rmi.Remote { MyObject msgsend (MyObject message) throws java.rmi.RemoteException; }
/* RemoteServer.java */ import java.rmi.*; import java.rmi.server.UnicastRemoteObject; public class RemoteServer extends UnicastRemoteObject implements RemoteInterface{ String name; static int messageCount; static String globalMessage=""; public RemoteServer(String name) throws RemoteException{ super(); this.name = name; }
public MyObject msgsend(MyObject message) throws RemoteException{ System.out.println("Server got[" + message.lenMsg() + "]:" + message.getMsg()); messageCount++; globalMessage=globalMessage+"\n"+message.getMsg(); message.setMsg("Total Message: "+ Integer.toString(messageCount)+globalMessage); return message; }
public static void main (String args[]){ System.out.println("Running Server ..."); System.setSecurityManager (new RMISecurityManager()); try{ String myName = "//"+args[0]+":"+args[1]+ "/ObjectServerTest"; RemoteServer theServer = new RemoteServer (myName); System.out.println("Server:rebining ..."); Naming.rebind(myName,theServer); } catch (Exception e){ System.out.println("An Exception occurred while creating server"); e.printStackTrace(); } } }