Skip to content

Building simple Client-Server system using RMI

Building simple Client-Server system using RMI

PROBLEM STATEMENT:

Write a program to implement an RMI application.

DESCRIPTION

DEFINTION OF RMI

Allows a Java method to obtain a reference to a remote object and
invoke methods of the remote object nearly as easily as if the
remote object existed locally

RMI APPLICATION

RMI STUBS AND SKELETONS

RMI uses stub and skeleton objects to provide the connection
between the client and the remote object
     A stub is a proxy for a remote object which is responsible for forwarding method invocations from the client to the server
where the actual remote object implementation resides
 A client’s reference to a remote object, therefore, is actually a reference to a local stub. The client has a local copy of the stub object.
 A skeleton is a server-side object which contains a method that dispatches calls to the actual remote object implementation
A remote object has an associated local skeleton object to
dispatch remote calls to it

SOURCE CODE

RMIPracIF.java

import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
public interface RMIPracIF extends Remote
    {
        public String welcome(String str)throws RemoteException;
       
    }

RMIIFImpl.java

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

public class RMIIFImpl extends UnicastRemoteObject implements RMIPracIF
{
   
       public RMIIFImpl()throws RemoteException{}

    public String welcome(String str)
    {
        String res=”Hi,” + str + ” have a great time in the Corporate World”;
        return res;
    }
}
   

RMIServer.java

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

public class RMIServer extends RMIIFImpl
    {
       
        RMIServer()throws Exception{}
        public static void main(String a[])throws Exception
    {
        RMIIFImpl ri=new RMIIFImpl();
        Naming.rebind(“StudName”,ri);
    System.out.println(“Server initiated….”);
    }
}   

RMIClient.java

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

public class RMIClient
{private static final String HOST_NAME=”localhost”;
 public static void main(String arg[]) throws Exception
    {
       
        try
        {
            RMIPracIF r = (RMIPracIF)Naming.lookup(“//”+HOST_NAME+”/StudName”);
            String s1 = r.welcome(“Priya”);
            System.out.println(s1);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

}

Step 1
Compile all 4 progs using command line tool javac

Step 2
Create stub and skeleton for the Implementation class using command line tool rmic -vcompat <Implclassfile>

Step 3
Start the rmiregistry on a different command window

Step 4
Run the Server and client program on two different command windows.


OUTPUT

C:priyaDCpracsSimpleRMI_prac8>java RMIServer
Server initiated….

C:priyaDCpracsSimpleRMI_prac8>java RMIClient
Hi,Priya have a great time in the Corporate World

1 thought on “Building simple Client-Server system using RMI”

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!