REMOTE METHOD INVOCATION - ADDITION/SUBTRACTION


AIM:
    To write a Java program for the implementation of Remote Method Invocation for addition and subtraction operations.

ALGORITHM:
 
SERVER SIDE:

Step 1: Start
Step 2: Define the class rmiserver
Step 3: Create the objects twox, twoy in try
Step 4: Register the objects twox, twoy
Step 5: Display the exception in catch
Step 6: Stop

CLIENT SIDE:
Step 1: Start
Step 2: Define the class rmiclient
Step 3: Initialize the string s1 & s2 in try
Step 4: Create the objects onex, oney
Step 5: Assign the value to m by calling the method add(4,2)
Step 6: Assign the value to n by calling the method sub(40,30)
Step 7: Display m & n
Step 8: Display the exception in catch
Step 9: Stop

SOURCE CODE:

one.java
import java.rmi.*;
interface one extends Remote
{

    public int add(int a, int b) throws RemoteException;
    public int sub(int a, int b) throws RemoteException;
}

two.java
import java.rmi.*;
import java.rmi.server.*;
public class two extends UnicastRemoteObject implements one
{
    public two() throws RemoteException { }
    public int add(int a, int b) throws RemoteException
    {
        System.out.println("Hello");
        return (a + b);
    }
    public int sub(int a, int b) throws RemoteException
    {
        System.out.println("Hello");
        return (a - b);
    }
}

rmiserver.java
import java.io.*;
import java.rmi.*;
import java.net.*;
public class rmiserver
{
    public static void main(String args[]) throws Exception
    {
        try
        {
            two twox = new two();
            Naming.bind("add", twox);
            System.out.println("Object registered");
            two twoy = new two();
            Naming.bind("sub", twoy);
            System.out.println("Object registered");
        }
        catch(Exception e)
        {
            System.out.println("Exception" + e);
        }
    }
}

rmiclient.java
import java.io.*;
import java.rmi.*;
import java.net.*;
public class rmiclient
{
    public static void main(String args[]) throws Exception
    {
        try
        {
            String s1 = "rmi://localhost/add";
            one onex = (one)Naming.lookup(s1);
            int m = onex.add(40,30);
            System.out.println("Addition : " + m);
            String s2 = "rmi://localhost/sub";
            one oney = (one)Naming.lookup(s2);
            int n = oney.sub(40, 30);
            System.out.println("Subtraction : " + n);
        }
        catch (Exception e)
        {
            System.out.println("Exception" + e);
        }
    }
}
         
OUTPUT: 

SERVER SIDE:


CLIENT SIDE:




















RESULT:
          Thus the Java program for the implementation of Remote Method Invocation for addition and subtraction was performed and the output was verified.

.
Previous
Next Post »

Still not found what you are looking for? Try again here.