Extra Programs

4
Simple RMI Application HelloImpl.java import java.rmi.*; public interface HelloImpl extends Remote { String sayHello(String a) throws RemoteException; } RMIHelloServer.java import java.rmi.*; import java.rmi.registry.*; import java.rmi.server.*; public class RMIHelloServer extends UnicastRemoteObject implements HelloImpl { public RMIHelloServer() throws RemoteException { System.out.println("The server is instantiated"); } public String sayHello(String a) { // Returns the square root of the number passed as argument String s; s="Hello, "+a; return s; } public static void main(String[] arguments) { try { // Instantiates the RMIServer object RMIHelloServer server = new RMIHelloServer(); /* The rebind() method takes two arguments, URL name and the Remote object which binds the remote object to the remote registry */ Naming.rebind("call1", server); /* The name call1 is a bind name given to the remote object, which gets registered with the RMI registry through which remote calls are made */ } catch (Exception exce) { System.out.println("Error -- " +exce.toString()); exce.printStackTrace(); } } // End of main() method } // End of RMIServer class RMIHelloClient.java import java.rmi.*; import java.rmi.registry.*; public class RMIHelloClient

description

Extra Programs

Transcript of Extra Programs

Page 1: Extra Programs

Simple RMI ApplicationHelloImpl.javaimport java.rmi.*;public interface HelloImpl extends Remote{String sayHello(String a) throws RemoteException;}

RMIHelloServer.javaimport java.rmi.*;import java.rmi.registry.*;import java.rmi.server.*;public class RMIHelloServer extends UnicastRemoteObject implements HelloImpl{public RMIHelloServer() throws RemoteException{System.out.println("The server is instantiated");}public String sayHello(String a){// Returns the square root of the number passed as argumentString s;s="Hello, "+a;return s;}public static void main(String[] arguments){try{// Instantiates the RMIServer objectRMIHelloServer server = new RMIHelloServer();/* The rebind() method takes two arguments, URL name and theRemote object which binds the remote object to the remoteregistry */Naming.rebind("call1", server);/* The name call1 is a bind name given to the remote object,which gets registered with the RMI registry through whichremote calls are made */}catch (Exception exce){System.out.println("Error -- " +exce.toString());exce.printStackTrace();}} // End of main() method} // End of RMIServer class

RMIHelloClient.javaimport java.rmi.*;import java.rmi.registry.*;public class RMIHelloClient{public static void main(String[] arguments){try{HelloImpl mthdIp =(HelloImpl)Naming.lookup("rmi://127.0.0.1/call1");/* '127.0.0.1' is the IP address of the Server machineand call1 is the name with which the Server Object isregistered. */

Page 2: Extra Programs

String s = mthdIp.sayHello(arguments[0]);System.out.println(s);}catch (Exception exec){System.out.println("Error -- " + exec.toString());exec.printStackTrace();}} //End of main() method} // End of RMIClient class

Complete following steps:1. Go to your working directory (F:\RMI\Hello)2. Write all Java programs. (Interface, server, client)3. Compile all java programs4. Once this has been completed, the stubs and skeletons for the Remote interfaces should be compiledby using the rmic stub compiler. The stub and skeleton of the example Remote interface arecompiled with the command:rmic RMIHelloServer

5. Open other command window. Go to directory where server files are kept and run serverjava RMIHelloServer

6. Open other command window. Go to directory where client files are kept and run clientjava RMIHelloClient Girish

Screen shots are shown below:8.2 Simple RMI Calculator ApplicationCalcImpl.javaimport java.rmi.*;public interface CalcImpl extends Remote{double add(int a, int b) throws RemoteException;double sub(int a, int b) throws RemoteException;double mul(int a, int b) throws RemoteException;double div(int a, int b) throws RemoteException;}

RMICalcServer.javaimport java.rmi.*;import java.rmi.registry.*;import java.rmi.server.*;public class RMICalcServer extends UnicastRemoteObject implements CalcImpl{public RMICalcServer() throws RemoteException{System.out.println("The server is instantiated");}public double add(int a, int b){return a+b;}public double sub(int a, int b){return a-b;}public double mul(int a, int b){return a*b;

Page 3: Extra Programs

}public double div(int a, int b){return (double)a/b;}public static void main(String[] arguments){try{// Instantiates the RMIServer objectRMICalcServer server = new RMICalcServer();/* The rebind() method takes two arguments, URL name and theRemote object which binds the remote object to the remoteregistry */Naming.rebind("call12", server);/* The name call1 is a bind name given to the remote object,which gets registered with the RMI registry through whichremote calls are made */}catch (Exception exce){System.out.println("Error -- " +exce.toString());exce.printStackTrace();}} // End of main() method} // End of RMIServer class

RMICalcClient.javaimport java.rmi.*;import java.rmi.registry.*;public class RMICalcClient{public static void main(String[] arguments){try{CalcImpl mthdIp =(CalcImpl)Naming.lookup("rmi://localhost/call12");int a = Integer.parseInt(arguments[0]);int b = Integer.parseInt(arguments[1]);double ans;ans = mthdIp.add(a,b);System.out.println(a+"+"+b+" = "+ans);ans = mthdIp.sub(a,b);System.out.println(a+"-"+b+" = "+ans);ans = mthdIp.mul(a,b);System.out.println(a+"*"+b+" = "+ans);ans = mthdIp.div(a,b);System.out.println(a+"/"+b+" = "+ans);}catch (Exception exec){System.out.println("Error -- " + exec.toString());exec.printStackTrace();}} //End of main() method} // End of RMIClient class