DOS Based Application which sends numbers to the RMI Server which calculates and sends the answer to the client. (Note: Client and Servers can be anywhere across the network). Find the codes below. It is a simple application showing how RMI(Remote Method Invocation) can be implemented.
Calculator.java
-----------------------------------------------------------------------------
import java.rmi.*;
public interface Calculator extends Remote
{
public int sum(int num1, int num2)throws RemoteException;
}
-----------------------------------------------------------------------------
CalClient.java
-----------------------------------------------------------------------------
import java.rmi.*;
public class CalClient
{
public static void main(String[] args)
{
try{
String url= "rmi://"+args[0]+"/Calculator";
Calculator cal = (Calculator)Naming.lookup(url);
int add = cal.sum(Integer.parseInt(args[1]),Integer.parseInt(args[2]));
System.out.println("The sum is: "+add);
}catch(Exception e){e.printStackTrace();}
}
}
-----------------------------------------------------------------------------
CalServer.java
-----------------------------------------------------------------------------
import java.rmi.*;
import java.rmi.server.*;
public class CalServer extends UnicastRemoteObject implements Calculator
{
public CalServer() throws RemoteException{}
public int sum(int num1, int num2)throws RemoteException
{
return (num1+num2);
}
public static void main(String[] args)
{
try{
CalServer cal = new CalServer();
Naming.bind("Calculator",cal);
System.out.println("Calculator Started ");
}catch(Exception e){e.printStackTrace();}
}
}
-----------------------------------------------------------------------------
No comments:
Post a Comment