RMI Program for arithmetic operations:
_________________________________________________________________________________
Interaface
package RMIDemo;
import java.rmi.Remote;
import java.rmi.RemoteException;
/**
*
* @author yogesh
*/
public interface one extends Remote{
public int add(int num1,int num2)throws RemoteException;
public int sub(int num1,int num2)throws RemoteException;
public int mul(int num1,int num2)throws RemoteException;
public int div(int num1,int num2)throws RemoteException;
}
EXECUTING DEMO IN NETBEANS
_________________________________________________________________________________
Interaface
package RMIDemo;
import java.rmi.Remote;
import java.rmi.RemoteException;
/**
*
* @author yogesh
*/
public interface one extends Remote{
public int add(int num1,int num2)throws RemoteException;
public int sub(int num1,int num2)throws RemoteException;
public int mul(int num1,int num2)throws RemoteException;
public int div(int num1,int num2)throws RemoteException;
}
_________________________________________________________________________________
Implementation Class
package RMIDemo;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
/**
*
* @author yogesh
*/
public class two extends UnicastRemoteObject implements one{
public two()throws RemoteException{}
@Override
public int add(int num1, int num2) throws RemoteException {
System.out.println("Addition Performed");
return ((num1+num2));
}
@Override
public int sub(int num1, int num2) throws RemoteException {
System.out.println("Subtraction Performed");
return ((num1-num2));
}
@Override
public int mul(int num1, int num2) throws RemoteException {
System.out.println("Multiplication Performed");
return ((num1*num2));
}
@Override
public int div(int num1, int num2) throws RemoteException {
System.out.println("Division Performed");
return ((num1/num2));
}
}
_________________________________________________________________________________
Server Class
package RMIDemo;
import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
/**
*
* @author yogesh
*/
public class rmiserver {
public static void main(String[] args) throws RemoteException, AlreadyBoundException, MalformedURLException, IOException {
Registry r = java.rmi.registry.LocateRegistry.createRegistry(1099);//1099 is the port number
Runtime.getRuntime().exec("cmd /c C:\\Program Files\\Java\\jdk1.8.0_144\\bin\\rmic C:\\Users\\Yogesh\\Documents\\NetBeansProjects\\DSLabs\\build\\classes\\RMIDemo.two");
two twox=new two();
Naming.bind("add", twox);
Naming.bind("sub", twox);
Naming.bind("mul", twox);
Naming.bind("div", twox);
System.out.println("ALL OBJECTS CREATED");
System.out.print("\nListing");
String s[]=Naming.list("two");
for(String str:s){
System.out.println(""+str);
}
}
}
_________________________________________________________________________________
Client Class
package RMIDemo;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Scanner;
/**
*
* @author yogesh
*/
public class rmiclient {
public static void main(String[] args) throws NotBoundException, MalformedURLException, RemoteException {
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter two Numbers:");
int num1=sc.nextInt();
int num2=sc.nextInt();
one onex=(one)Naming.lookup("rmi://localhost/add");
one oney=(one)Naming.lookup("rmi://localhost/add");
one onez=(one)Naming.lookup("rmi://localhost/add");
one onew=(one)Naming.lookup("rmi://localhost/add");
System.out.println("Addition="+onex.add(num1,num2));
System.out.println("Subtraction="+oney.sub(num1,num2));
System.out.println("Multiplication="+onez.mul(num1,num2));
System.out.println("Division="+onew.div(num1,num2));
}
}
_________________________________________________________________________________
EXECUTING DEMO IN NETBEANS