11.REMOTE METHOD INVOCATION

  //Define the remote interface import java.rmi.Remote; import java.rmi.RemoteException; public interface Hello extends Remote { void printMsg() throws RemoteException; } //Develop the implementation class (remote object) // Implementing the remote interface public class ImplExample implements Hello { public void printMsg() { System.out.println("This is an example RMI program"); } } //Develop the server program import java.rmi.registry.Registry; import java.rmi.registry.LocateRegistry; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class Server extends ImplExample { public Server() {} public static void main(String args[]) { try { ImplExample obj = new ImplExample(); Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0); Registry registry = LocateRegistry.getRegistry(); registry.bind("Hello", stub); System.err.println("Server ready"); } catch (Exception e) { System.err.println("Server exception: " + e.toString()); e.printStackTrace(); } } } //Develop the client program import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; public class Client { private Client() {} public static void main(String[] args) { try { Registry registry = LocateRegistry.getRegistry(null); Hello stub = (Hello) registry.lookup("Hello"); stub.printMsg(); } catch (Exception e) { System.err.println("Client exception: " + e.toString()); e.printStackTrace(); } } }





Comments