Code for Develop a RMI based application for addition of two complex numbers in Java
----------------------------------------------------------------------------------
Interface
----------------------------------------------------------------------------------
publicinterface sumInterface extends java.rmi.Remote
{
int find_sum(int n1,int n2) throws java.rmi.RemoteException;
}
-------------------------------------------------------------------------------------------------
Implementation
-------------------------------------------------------------------------------------------------
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.UnicastRemoteObject;
publicclass sumImpl extends UnicastRemoteObject
implements sumInterface
{
public sumImpl(String name) throws RemoteException
{
super();
try
{
Naming.rebind(name,this);
}
catch(Exception e) { }
}
publicint find_sum(int n1,int n2) throws RemoteException
{
int ans=0;
ans = n1+n2;
return ans;
}
}
-------------------------------------------------------------------------------------------------
Client
-------------------------------------------------------------------------------------------------
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.io.DataInputStream;
publicclass sumClient
{
publicstaticvoid main(String args[])
{
System.setSecurityManager(new RMISecurityManager());
try
{
int ans;
int intNumber1 = 0;
int intNumber2 = 0;
sumInterface pi = (sumInterface) Naming.lookup("//localhost/pno");
DataInputStream in = new DataInputStream(System.in);;
System.out.print("Enter Number 1 : ");
intNumber1 = Integer.parseInt(in.readLine());
System.out.print("Enter Number 2 : ");
intNumber2 = Integer.parseInt(in.readLine());
ans = pi.find_sum(intNumber1,intNumber2);
System.out.println(ans);
}
catch(Exception e) { }
}
}
-------------------------------------------------------------------------------------------------
Server
-------------------------------------------------------------------------------------------------
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
publicclass sumServer
{
publicstaticvoid main(String args[])
{
System.setSecurityManager(new RMISecurityManager());
try
{
sumImpl pim = new sumImpl("//localhost/pno");
System.out.println("\nServer is ready...");
}
catch(Exception e) { }
}
}
-------------------------------------------------------------------------------------------------
Policy
-------------------------------------------------------------------------------------------------
grant
{
// Allow everything for now
permission java.security.AllPermission;
};
-------------------------------------------------------------------------------------------------
Output
-------------------------------------------------------------------------------------------------
Enter Number 1 : 5
Enter Number 2 : 8
13