Calculator.java
|
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Calculator
extends Remote
{
public long add(long a,long b)
throws RemoteException;
}
|
CalculatorImpl.java
|
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class CalculatorImpl
extends UnicastRemoteObject implements Calculator
{
protected CalculatorImpl()
throws RemoteException
{
super();
}
public long add(long a,
long b) throws RemoteException
{
return a+b;
}
}
|
CalculatorServer.java
21
|
import java.rmi.Naming;
public class CalculatorServer
{
CalculatorServer()
{
try
{
Calculator
c = new CalculatorImpl();
Naming.rebind("rmi://127.0.0.1:1099/CalculatorService",
c);
}
catch (Exception
e)
{
e.printStackTrace();
}
}
public static void main(String[]
args)
{
new CalculatorServer();
}
}
|
CalculatorClient.java
|
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.add(10, 15));
}
catch (Exception
e)
{
System.out.println(e);
}
}
}
|
See more at:
http://www.java2all.com/1/5/22/113/Technology/RMI/RMI-Program/RMI-Program-in-java#sthash.bbdLDMkn.dpuf
No comments:
Post a Comment