© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu
Quick Links
Easy Studies

RMI Program

Description:

RMI (Remote Method Invocation) is the action of invoking a method of a remote interface on a remote object. RMI’s purpose is to make objects in separate JVMs look and act like local objects .A remote object is always accessed via its remote interface, in other words the client invokes methods on the object only after casting the reference to the remote interface.

This program has an RmiClient and RmiServer classes and also has RmiInterface and RmiImpl class. RMI server can call the implementation class for the interface which has been provided between the client and Server. Server binds with rmiregistry then only it can accept any request from the Client. RmiClient creates the object for interface by giving the server’s URI. Then call the interface method, this call is redirected to the remote object. Implementation class provides the result for the user’s method invocation through interface.

RMI Program:

/*RmiClient.java*/ import java.rmi.*; import java.io.*; public class RmiClient { public static void main (String args []) { BufferedReader reader; try { //String url="rmi: //"+args [0] +"/rmi"; RmiInterface rmiinter= (RmiInterface)Naming.lookup("abc"); System.out.println ("\n The first no is"+args[0]); System.out.println ("\n The second no is"+args[1]); double d1, d2; d1=Double.valueOf (args [0]).doubleValue (); d2=Double.valueOf (args [1]).doubleValue (); System.out.println ("\n The sum of two values"+rmiinter.add (d1, d2)); } catch (Exception e) { System.out.println ("An error occurred."+e); } } } /*RmiImpl.java*/ import java.rmi.*; import java.rmi.server.*; public class RmiImpl extends UnicastRemoteObject implements RmiInterface { public RmiImpl () throws RemoteException { } public double add (double d1, double d2) throws Exception { return d1+d2; } } /*RmiInterface.java*/ import java.rmi.*; public interface RmiInterface extends Remote { double add (double d1, double d2) throws Exception; } /*RmiServer.java*/ import java.rmi.*; import java.io.*; public class RmiServer { public static void main (String args []) { try { RmiImpl obj=new RmiImpl (); Naming.rebind ("abc", obj); } catch (Exception e) { System.out.println ("An error occurred trying to "+ "bind the object to the registry."); } } }

Sample ScreenShot:

 
SLogix Student Projects

⇓Student Projects⇓
⇑Student Projects⇑
bottom