RMI Addition

Write a network program to implement addition using RMI.

The program can be implemented using four files:

AddServerIntf.java


import java.rmi.*;
public interface AddServerIntf extends Remote
{
	double add(double d1, double d2) throws RemoteException;
}

AddServerImpl.java


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;
	}
}

AddServer.java


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);
		}
	}
}

AddClient.java


import java.io.*;
import java.rmi.*;
public class AddClient
{
	public static void main(String args[]) throws Exception
	{
		try
		{
			String addServerURL="rmi://127.0.0.1/AddServer";
			AddServerIntf addServerIntf= (AddServerIntf)
Naming.lookup(addServerURL);
			BufferedReader br= new BufferedReader(new
InputStreamReader(System.in));

			System.out.println("Enter the first value: ");
			Double d1=Double.parseDouble(br.readLine());

			System.out.println("Enter the second value: ");
			Double d2=Double.parseDouble(br.readLine());

System.out.println(d1+"+"+d2+"="+addServerIntf.
add(d1,d2));
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
	}
}

Output

 

Terminal 1 Terminal 2
>java Server >java Client>Enter the first value: 10

>Enter the Second value: 20

>10+20=30

 

 


	

Leave a Reply

Your email address will not be published. Required fields are marked *