1 / 18

Java RMI Example: Client-Server Communication Structure

This document provides a detailed overview of a Java RMI example demonstrating client-server communication. It outlines the necessary classes, including `MyObject`, `RemoteServer`, `RemoteInterface`, `RemoteClient1`, and `RemoteClient2`. The server listens on a specified TCP port and prepares to handle requests from multiple clients. The code includes the setup for running RMI registry and the client-server interaction flow where clients send serialized objects to the server, which processes the data and replies. Configuration settings for security policies and IP addresses are also specified.

dirk
Télécharger la présentation

Java RMI Example: Client-Server Communication Structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. RMI Example

  2. 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

  3. 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

  4. Java policy for RMI grant { permission java.net.SocketPermission "*:1024-65535", "connect,accept"; permission java.net.SocketPermission "*:80","connect"; };

  5. start rmiregistry $ rmiregistery 1111 & Default TCP port: 1099

  6. 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

  7. 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

  8. 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) ====== ============ ======== ===== ==============

  9. 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> *****************************************************

  10. 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> *****************************************************

  11. 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 *****************************************************

  12. /* 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; } }

  13. /* 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"); } } }

  14. /* 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"); } } }

  15. /* RemoteInterface.java */ public interface RemoteInterface extends java.rmi.Remote { MyObject msgsend (MyObject message) throws java.rmi.RemoteException; }

  16. /* 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; }

  17. 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; }

  18. 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(); } } }

More Related