Remote Method Invocation (RMI) is an API which allows an object to invoke a method on an object that exists in another address space, which could be on the same machine or on a remote machine. Through RMI, object running in a JVM present on a computer (Client side) can invoke methods on an object present in another JVM (Server side). RMI creates a public remote server object that enables client and server side communications through simple method calls on the server object. RMI is used for building distributed application.
The RMI provides remote communication between the applications using two objects Stub and Skeleton.
The stub is an object, acts as a gateway for the client side. All the outgoing requests are routed through it. It resides at the client side and represents the remote object
Skeleton:
The skeleton is an object, acts as a gateway for the server side object. All the incoming requests are routed through it.
Two Intermediate objects are used for the communication between client and server [Stubs and Skeleton].
Steps to Implement RMI:
Source Code:
Filename: MyInterface.java
Filename: MyServer.java
import java.rmi.*;
import java.util.*;
import java.text.*;
import java.rmi.server.*;
public class MyServer extends UnicastRemoteObject implements MyInterface
{
}
Filename: Register.java
import java.rmi.*;
import java.rmi.registry.*; public class Register
{
• Registry reg=LocateRegistry.createRegistry(2099); MyServer obj=new MyServer(); Naming.rebind(“rmi://localhost:2099/dt”,obj);
• }
}
Filename: MyClient.java
import java.rmi.*;
public class MyClient
{
public static void main(String[] args)
{
}
Source Code:
Filename: MyInterface.java
import java.rmi.*;
public interface MyInterface extends Remote
{
public int solveEq1(int a,int b) throws RemoteException; public int solveEq2(int a,int b) throws RemoteException;
}
Filename: MyServer.java
import java.rmi.*;
import java.rmi.server.*;
import java.math.*;
public class MyServer extends UnicastRemoteObject implements MyInterface
{
public MyServer()throws RemoteException
{
}
public int solveEq1(int a,int b) throws RemoteException
{
}
public int solveEq2(int a,int b) throws RemoteException
{
}
}
Filename: Register.java
import java.rmi.*;
import java.rmi.registry.*; public class Register
{
public static void main(String[] args)
{
try {
Registry reg=LocateRegistry.createRegistry(2099); MyServer obj=new MyServer(); Naming.rebind(“rmi://localhost:2099/equ”,obj);
}
}}
Filename: MyClient.java
import java.rmi.*;
import java.util.*;
public class MyClient
{
public static void main(String[] args)
{
try {
int num1,num2,num3,res1,res2;
MyInterface object=(MyInterface)Naming.lookup(“rmi://localhost:2099/eq”); Scanner in= new Scanner(System.in);
System.out.println(“Enter 1st number”); num1=in.nextInt(); System.out.println(“Enter 2st number”); num2=in.nextInt();
System.out.println(“(a+b)2 =” + object.solveEq1(num1,num2);); System.out.println(“(a+b)3 =” + object.solveEq2(num1,num2););
}
catch(Exception e) {}
}
}
Source Code:
Filename: MyInterface.java
import java.rmi.*;
public interface MyInterface extends Remote
{
public int find(int a,int b) throws RemoteException;
}
Filename: MyServer.java
import java.rmi.*;
import java.rmi.server.*;
public class MyServer extends UnicastRemoteObject implements MyInterface
Filename: Register.java
import java.rmi.*;
import java.rmi.registry.*; public class Register
{
Filename: MyClient.java
import java.rmi.*;
import java.awt.*;
import java.awt.event.*; import javax.swing.*;
public class MyClient extends JFrame implements ActionListener
{
JTextField tf1,tf2;
JButton btn;
JLabel lb,lb1,lb2;
MyClient()
{
}
public void actionPerformed(ActionEvent ae)
{
Exercise 1: The client should provide the values of a, b & c. The server will solve the equation (ax2 + bx + c = 0) and will give back the value of x. If a = 1, b = 5 and c = 6 then return value will be x = -2 or x = -3.
Exercise 2: Design a Graphical User Interface (GUI) based Basic calculator by