Server Side program
import java.rmi.*;
import java.rmi.server.*;
interface RMI_Intf extends Remote
{
public int add(int a,int b) throws RemoteException;
}
class RMI_Server extends UnicastRemoteObject implements RMI_Intf
{
RMI_Server()throws RemoteException
{
}
public int add(int a,int b)throws RemoteException
{
System.out.println("Press To Start");
try{System.in.read();}catch(Exception e){}
System.out.println("Client passed values "+a+" and "+b);
try{Thread.sleep(2000);}catch(Exception e){}
int c=a+b;
System.out.println("Addition is "+c);
return c;
}
}
class ServerSide
{
public static void main(String ar[])
{
try{
RMI_Server obj=new RMI_Server();
Naming.rebind("call",obj);
System.out.println("Binding Complete");
}catch(Exception e)
{
System.out.println("Exception :"+e);
}
}
}
Client Side Program
import java.rmi.*;
class ClientSide
{
public static void main(String ar[])
{
try{
RMI_Intf remote_obj=(RMI_Intf)Naming.lookup("call");
System.out.println("Sub of 100, 200 is "+(remote_obj.add(100,200)));
}catch(Exception e)
{
System.out.println("Exception :"+e);
}
}
}
OUTPUT
First start rmiregistry application as following
after that run server-side program and then client program, you will see following output
import java.rmi.server.*;
interface RMI_Intf extends Remote
{
public int add(int a,int b) throws RemoteException;
}
class RMI_Server extends UnicastRemoteObject implements RMI_Intf
{
RMI_Server()throws RemoteException
{
}
public int add(int a,int b)throws RemoteException
{
System.out.println("Press To Start");
try{System.in.read();}catch(Exception e){}
System.out.println("Client passed values "+a+" and "+b);
try{Thread.sleep(2000);}catch(Exception e){}
int c=a+b;
System.out.println("Addition is "+c);
return c;
}
}
class ServerSide
{
public static void main(String ar[])
{
try{
RMI_Server obj=new RMI_Server();
Naming.rebind("call",obj);
System.out.println("Binding Complete");
}catch(Exception e)
{
System.out.println("Exception :"+e);
}
}
}
Client Side Program
import java.rmi.*;
class ClientSide
{
public static void main(String ar[])
{
try{
RMI_Intf remote_obj=(RMI_Intf)Naming.lookup("call");
System.out.println("Sub of 100, 200 is "+(remote_obj.add(100,200)));
}catch(Exception e)
{
System.out.println("Exception :"+e);
}
}
}
OUTPUT
First start rmiregistry application as following
after that run server-side program and then client program, you will see following output
Comments