Java - Mathematical Operations using RMI

MathServerIntf.java

import java.rmi.*;
import java.rmi.server.*;

public interface MathServerIntf extends Remote {   

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

    public int subtract(int a, int b) throws RemoteException;

    public int multiply(int a, int b) throws RemoteException;

    public int divide(int a, int b) throws RemoteException;
}



MathServerImpl.java

import java.rmi.*;
import java.rmi.server.*;

public class MathServerImpl extends UnicastRemoteObject implements MathServerIntf {

    public MathServerImpl() throws RemoteException {
    }

    public int add(int a, int b) throws RemoteException {
        return a+b;
    }

    public int subtract(int a, int b) throws RemoteException {
        return a-b;
    }

    public int multiply(int a, int b) throws RemoteException {
        return a*b;
    }

    public int divide(int a, int b) throws RemoteException {
        if(b==0) {
            return -1;
        } else {
            return a/b;
        }
    }
}



MathServer.java

import java.rmi.*;
import java.rmi.server.*;

public class MathServer {

    public static void main(String[] args) {
        try {
            MathServerImpl mathServerImpl = new MathServerImpl();
            Naming.rebind("MathServer", mathServerImpl);
        } catch(Exception exp) {
            exp.printStackTrace();
        }
    }
}



MathClient.java

import java.rmi.*;
import java.rmi.server.*;
import java.io.*;

public class MathClient {

    public static void main(String[] args) {
        try {
            String mathServerURL = "rmi://" + args[0] + "/MathServer";
            MathServerIntf mathServerIntf = (MathServerIntf)
                Naming.lookup(mathServerURL);
            BufferedReader bufferedReader = new BufferedReader(
                                new InputStreamReader(System.in));

            while(true) {
           
            System.out.println("\n\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. Exit\n");
            System.out.println("Enter your option...");
            int option = Integer.parseInt(bufferedReader.readLine());
           
            int number1 = 0, number2 = 0;

            if(option>=1 && option <=4) {
                System.out.println("Enter the first number...");
                number1 = Integer.parseInt(bufferedReader.readLine());

                System.out.println("Enter the second number...");
                number2 = Integer.parseInt(bufferedReader.readLine());
            }

            switch(option) {
                case 1: System.out.println("Sum...");
                    System.out.println(
                        mathServerIntf.add(number1, number2));
                    break;

                case 2: System.out.println("Difference...");
                    System.out.println(
                        mathServerIntf.subtract(number1, number2));
                    break;
       
                case 3: System.out.println("Product...");
                    System.out.println(
                        mathServerIntf.multiply(number1, number2));
                    break;
       
                case 4: System.out.println("Quotient...");
                    System.out.println(
                        mathServerIntf.divide(number1, number2));
                    break;

                case 5: System.exit(0);

                default: System.out.println("Enter a valid option...");               
            }

            }
           
                                   
        } catch(Exception exp) {
            exp.printStackTrace();
        }
    }
}



Compile the source code
> javac *.java

Create the stub class using RMI compiler
> rmic MathServerImpl

The server part consists of:
MathServerIntf.class
MathServerImpl.class
MathServer.class
MathServerImpl_Stub.class

The client part consists of:
MathClient.class
MathServerImpl_Stub.class
MathServerIntf.class


Start the RMI registry
> start rmiregistry

Run the server
> java MathServer


Run the client
> java MathClient 127.0.0.1
 



Post a Comment

0 Comments