Google Ads
Thursday, May 21, 2009
RMI Method -Program
PROGRAM FOR INTERFACE
import java.rmi.*;
public interface AddServerIntf extends Remote
{
double add(double d1,double d2) throws RemoteException;
}
/* PROGRAM FOR ADDSERVER IMPLEMENTS*/
import java.rmi.*;
import java.rmi.server.*;
public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf
{
public AddServerImpl() throws RemoteException{}
public double add(double d1,double d2) throws RemoteException
{
return d1+d2;
}
}
PROGRAM FOR SERVER
import java.net.*;
import java.rmi.*;
public class AddServer
{
public static void main(String args[])
{
try
{
AddServerImpl addServerImpl=new AddServerImpl();
Naming.rebind("AddServer",addServerImpl);
}
catch(Exception e)
{
System.out.println("Exception: "+e);
}
}
}
PROGRAM FOR CLIENT
import java.rmi.*;
public class AddClient
{
public static void main(String args[])
{
try
{
String addServerURL="rmi://"+args[0]+"/AddServer";
AddServerIntf addServerIntf=(AddServerIntf)Naming.lookup(addServerURL);
System.out.println("The first number is: "+args[1]);
double d1=Double.valueOf(args[1]).doubleValue();
System.out.println("The second number is: "+args[2]);
double d2=Double.valueOf(args[2]).doubleValue();
System.out.println("The sum is: "+addServerIntf.add(d1,d2));
}
catch(Exception e)
{
System.out.println("Exception: "+e);
} } }
OUTPUT:
Z:\>cd jdk1.4
Z:\jdk1.4>cd bin
Z:\jdk1.4\bin>javac AddServerIntf.java
Z:\jdk1.4\bin>javac AddServerImpl.java
Z:\jdk1.4\bin>rmic AddServerImpl
Z:\jdk1.4\bin>javac AddServer.java
Z:\jdk1.4\bin>javac AddClient.java
Z:\jdk1.4\bin>start rmiregistry
Z:\jdk1.4\bin>java AddServer
Z:\>cd jdk1.4
Z:\jdk1.4>cd bin
Z:\jdk1.4\bin>java AddClient - you type " IP address "
The first number is: 24
The second number is: 17
The sum is: 41.0
Z:\jdk1.4\bin>
Subscribe to:
Post Comments (Atom)
thanks
ReplyDelete