RMI simple example
By karthick
, in
RMI simple example
,
0 Comments
RMI simple example
Interface
import
java.rmi.Remote;
import java.rmi.RemoteException;
public interface Calculator
extends Remote
{
public long addition(long a,long b) throws
RemoteException;
public long subtraction(long a,long b)
throws RemoteException;
public long multiplication(long a,long b)
throws RemoteException;
public long division(long a,long b) throws
RemoteException;
}
Implementation
import java.rmi.RemoteException;
import
java.rmi.server.UnicastRemoteObject;
public class CalculatorImpl
extends UnicastRemoteObject implements Calculator
{
protected CalculatorImpl() throws
RemoteException
{
super();
}
public long addition(long a, long b) throws
RemoteException
{
return a+b;
}
public long subtraction(long a, long b)
throws RemoteException
{
return a-b;
}
public long multiplication(long a, long b)
throws RemoteException
{
return a*b;
}
public long division(long a, long b) throws
RemoteException
{
return a/b;
}
}
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Calculator
extends Remote
{
public long addition(long a,long b) throws
RemoteException;
public long subtraction(long a,long b)
throws RemoteException;
public long multiplication(long a,long b)
throws RemoteException;
public long division(long a,long b) throws
RemoteException;
}
Server
import java.rmi.Naming;
public class CalculatorServer
{
CalculatorServer()
{
try
{
Calculator c = new
CalculatorImpl();
Naming.rebind("rmi://localhost:1099/CalculatorService", c);
}
catch (Exception e)
{
System.out.println("Exception is :
"+e);
}
}
public static void main(String[] args)
{
new CalculatorServer();
}
}
Client
import java.rmi.Naming;
public class CalculatorClient
{
public static void main(String[] args)
{
try
{
Calculator c = (Calculator)
Naming.lookup("//127.0.0.1:1099/CalculatorService");
System.out.println("Addition :
"+c.addition(10,5));
System.out.println("Subtraction : "+c.subtraction(10,5));
System.out.println("Multiplication
:"+c.multiplication(10,5));
System.out.println("Division : "+c. division(10,5));
}
catch (Exception e)
{
System.out.println("Exception
is : "+e);
}
}
}
Compilation
javac Calculator.java
javac CalculatorImpl.java
javac CalculatorServer.java
javac CalculatorClient.java
rmic CalculatorImpl
start rmiregistry
java CalculatorServer
java CalculatorClient
output
Addition : 15
Subtraction : 5
Multiplication :50
Division : 2
0 Response to "RMI simple example"
Post a Comment