Remote Method Invocation

REMOTE METHOD INVOCATION


// Display a string using Remote Method Invocation.


Steps to implement RMI:
1.    Start the program.
2.    Create and compile the following four source files
                     i.The interface file defines the remote interface that is provided by the server.
                   ii.The implementation file implements the remote interface
                  iii.The server file contains the main program for the server machine to update the RMI registry on that machine
                  iv.The client file implements client side of the distributed application by importing package.
3.    Generate a stub using RMI compiler as
rmic < implementation filename >
4.    Start the rmiregistry on the server machine as
start rmiregistry
5.    Execute the server program.
6.    Pass the necessary parameters and execute the client program.
7.    Verify the output.
8.    Stop the program.



PROGRAM:


// INTERFACE PROGRAM
// DispServerIntf.java

import java.rmi.*;

public interface DispServerIntf extends Remote
{
     String Display(String S) throws RemoteException;
}


// IMPLEMENTATION PROGRAM
// DispServerImpl.java

import java.rmi.*;
import java.rmi.server.*;

public class DispServerImpl extends UnicastRemoteObject implements DispServerIntf
{
     public DispServerImpl() throws RemoteException
     {
     }

     public String Display(String S) throws RemoteException
     {
          return ("WELCOME "+S+" ! ");
     }
}


// SERVER PROGRAM
// DispServer.java

import java.net.*;
import java.rmi.*;

public class DispServer
{
     public static void main(String args[])
     {
          try
          {
              DispServerImpl DispServerImpl = new DispServerImpl();
              Naming.rebind("DispServer", DispServerImpl);
          }
          catch(Exception e)
          {
              System.out.println("Exception: " + e);
          }
     }
}


// CLIENT PROGRAM
// DispClient.java

import java.rmi.*;

public class DispClient
{
     public static void main(String args[])
     {
          try
          {
              String DS_URL = "rmi://" + args[0] + "/DispServer";
              DispServerIntf DSIntf=(DispServerIntf)Naming.lookup(DS_URL);

              System.out.println("\n\n"+DSIntf.Display(args[1])+"\n");
          }

          catch(Exception e)
          {
              System.out.println("Exception: " + e);
          }
     }
}



OUTPUT:


SERVER WINDOW

> javac DispServerIntf.java
> javac DispServerImpl.java
> javac DispServer.java
> rmic DispServerImpl
> start rmiregistry
> java DispServer


RMIREGISTRY





CLIENT WINDOW

> javac DispClient.java
> java DispClient localhost "to Java"

WELCOME to Java !

>















No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu