Anda di halaman 1dari 7

Code:

// Program to create a ServerInterface extending Remote and declaration of functions.

import java.rmi.*;

public interface ServerInterface extends Remote


{
int Add(int n1, int n2) throws RemoteException;
int Sub(int n1, int n2) throws RemoteException;
int Mult(int n1, int n2) throws RemoteException;
int Div(int n1, int n2) throws RemoteException;
}

// Program to implement the ServerInterface which helps to create a server stub

import java.rmi.*;
import java.rmi.server.*;
import java.lang.Math.*;

public class Server extends UnicastRemoteObject implements ServerInterface


{
public Server() throws RemoteException
{
}
public int Add(int n1, int n2) throws RemoteException
{
return n1+n2;
}
public int Sub(int n1, int n2) throws RemoteException
{
return n1-n2;
}
public int Mult(int n1, int n2) throws RemoteException
{
return n1*n2;
}
public int Div(int n1, int n2) throws RemoteException
{
return n1/n2;
}
}

// Program to Test the server

import java.rmi.*;

public class TestServer


{
public static void main(String ar[])
{
try
{
Server server = new Server();
Naming.rebind("TestServer",server);
}
catch(Exception e)
{
System.out.println(e);
}
}
}

// Program to run the client using rmi

import java.rmi.*;

public class Client


{
public static void main(String ar[])
{
if(ar.length != 3)
{
System.out.println("Usage: java <hostname> <number1>
<number2>");
System.exit(1);
}
try
{
String url = "rmi://" + ar[0] + "/TestServer";
ServerInterface obj = (ServerInterface) Naming.lookup(url);

int n1 = Integer.parseInt(ar[1]);
int n2 = Integer.parseInt(ar[2]);

System.out.println("\n The addition is::" + obj.Add(n1,n2));


System.out.println("\n The subtraction is::" + obj.Sub(n1,n2));
System.out.println("\n The multiplication is::"+
obj.Mult(n1,n2));
System.out.println("\n The division is::"+ obj.Div(n1,n2));
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Steps for executing:

1. Compile all server side files


2. rmic <server file> which implements the RMI interface
3. Copy the stub and the class files from the server to the client
4. Compile the client file
5. Start rmiregistry
6. Run the server file
7. Run the client file

Output:

D:\Server>javac *.java

D:\Server>rmic Server

D:\Server>start rmiregistry

D:\Server>java TestServer

D:\Client>javac Client.java

D:\Client>java Client localhost 4 2

The addition is::6

The subtraction is::2

The multiplication is::8

The division is::2

Class Diagram:
Class Description:
The program above is basically used for demonstrating the RMI concept which helps in
remote method invocation of the server methods from the client through the use of server
stub. The program consists of 3 classes and 1 interface. Firstly, we have the interface
ServerInterface which extends an existing interface Remote so as to provide the remote
methods. Then, we have the Sever class that extends from UnicastRemoteObject which
helps in the creation of the stub which is used in the client side so as to enable remote
method invocation. And Server class implements SeverInterface, thus implementing all the
methods defined in SeverInterface. Class TestServer is basically used to test the server that
we have creating by creating an object of Sever class and then rebind it. Finally, we have
the class Client which creates an object of ServerInterface and using this objects and the
server stub gets access to all the methods thus demonstrating Remote Method Invocation.

Interface Used:

public interface Remote


The Remote interface serves to identify interfaces whose methods may be invoked from a
non-local virtual machine. Any object that is a remote object must directly or indirectly
implement this interface. Only those methods specified in a "remote interface", an interface
that extends java.rmi.Remote are available remotely.

Class Used:

public class UnicastRemoteObject extends RemoteServer


Used for exporting a remote object with JRMP and obtaining a stub that communicates to
the remote object.

public final class Naming extends Object


The Naming class provides methods for storing and obtaining references to remote objects
in a remote object registry. Each method of the Naming class takes as one of its arguments
a name that is a java.lang.String in URL format (without the scheme component) of the
form:
//host:port/name
Where host is the host (remote or local) where the registry is located, port is the port
number on which the registry accepts calls, and where name is a simple string
uninterpreted by the registry.

Methods Used:

public static void rebind(String name, Remote obj) throws RemoteException,


MalformedURLException
Rebinds the specified name to a new remote object. Any existing binding for the name is
replaced.

public static Remote lookup(String name) throws NotBoundException,


MalformedURLException, RemoteException
Returns a reference, a stub, for the remote object associated with the specified name.

Anda mungkin juga menyukai