Remote Method Invocation RMI program code using Java



The program is written 3 files namely :RMI.java , RMIserver.java, RMIclient.java. This program implements RMI concept in java .
The Java Remote Method Invocation Application Programming Interface, or Java RMI, is a Java API that performs the object-oriented equivalent of remote procedure calls , with support for direct transfer of serialized Java objects and distributed garbage collection.

CODE:


RMI.java

import java.rmi.*;

public interface RMI extends Remote
{
void receiveMessage(String x) throws RemoteException;
}




RmiServer.java

import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.net.*;

public class RmiServer extends java.rmi.server.UnicastRemoteObject
implements RMI
{
    int      thisPort;
    String   thisAddress;
    Registry registry;    // rmi registry for lookup the remote objects.

    // This method is called from the remote client by the RMI.
    // This is the implementation of the “ReceiveMessageInterface”.
    public void receiveMessage(String x) throws RemoteException
    {
        System.out.println(x);
    }

    public RmiServer() throws RemoteException
    {
        try{
            // get the address of this host.
            thisAddress= (InetAddress.getLocalHost()).toString();
        }
        catch(Exception e){
            throw new RemoteException("Can't get inet address.");
        }
        thisPort=3232;  // this port(registry’s port)
        System.out.println("This address="+thisAddress+",port="+thisPort);
        try{
        // create the registry and bind the name and object.
        registry = LocateRegistry.createRegistry( thisPort );
            registry.rebind("rmiServer", this);
        }
        catch(RemoteException e){
        throw e;
        }
    }
  
    static public void main(String args[])
    {
        try{
        RmiServer s=new RmiServer();
    }
    catch (Exception e) {
           e.printStackTrace();
           System.exit(1);
    }
     }
}


RmiClient.java

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

public class RmiClient
{
    static public void main(String args[])
    {
       RMI rmiServer;
       Registry registry;
       String serverAddress=args[0];
       String serverPort=args[1];
       String text=args[2];
       System.out.println("Sending "+text+" to "+serverAddress+":"+serverPort);
       try{
           // get the “registry”
           registry=LocateRegistry.getRegistry(
               serverAddress,
               (new Integer(serverPort)).intValue()
           );
           // look up the remote object
           rmiServer=
              (RMI)(registry.lookup("rmiServer"));
           // call the remote method
           rmiServer.receiveMessage(text);
       }
       catch(RemoteException e){
           e.printStackTrace();
       }
       catch(NotBoundException e){
           e.printStackTrace();
       }
    }
}





OUTPUT:



Server Output:-
C:\Program Files\Java\jdk1.5.0\bin>javac RMI.java

C:\Program Files\Java\jdk1.5.0\bin>javac RmiServer.java

C:\Program Files\Java\jdk1.5.0\bin>rmic RmiServer

C:\Program Files\Java\jdk1.5.0\bin>java RmiServer
This address=i3-05/192.168.100.180,port=3232
Hi,
This
is
Ascyena


Client Output:-
C:\Program Files\Java\jdk1.5.0\bin>javac RmiClient.java

C:\Program Files\Java\jdk1.5.0\bin>java RmiClient 192.168.100.180 3232 Hi,
Sending Hi, to 192.168.100.180:3232

C:\Program Files\Java\jdk1.5.0\bin>java RmiClient 192.168.100.180 3232 This
Sending This to 192.168.100.180:3232

C:\Program Files\Java\jdk1.5.0\bin>java RmiClient 192.168.100.180 3232 is
Sending is to 192.168.100.180:3232

C:\Program Files\Java\jdk1.5.0\bin>java RmiClient 192.168.100.180 3232 Ascyena
Sending Ascyena to 192.168.100.180:3232

0 comments:

Post a Comment

If You Are Asking Some Question On This Comment Then Click On Subscribe by email Link