0% found this document useful (0 votes)
47 views3 pages

RMI Calculator

The document outlines the steps to create a remote calculator application using Java RMI. It includes defining a remote interface, implementing the interface on the server, creating the server and client classes, and instructions for compiling and running the program. The calculator supports basic operations: addition, subtraction, multiplication, and division, with error handling for division by zero.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views3 pages

RMI Calculator

The document outlines the steps to create a remote calculator application using Java RMI. It includes defining a remote interface, implementing the interface on the server, creating the server and client classes, and instructions for compiling and running the program. The calculator supports basic operations: addition, subtraction, multiplication, and division, with error handling for division by zero.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Step 1: Define the Remote Interface

Create a file Calculator.java to define the methods that the server will implement.

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Calculator 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;
}

Step 2: Implement the Remote Interface on the Server

Create a file CalculatorImpl.java that implements the Calculator interface.

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class CalculatorImpl extends UnicastRemoteObject implements


Calculator {

protected CalculatorImpl() throws RemoteException {


super();
}

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) throw new ArithmeticException("Division by zero");
return a / b;
}
}

Step 3: Create the Server

Create a file CalculatorServer.java that registers the CalculatorImpl object.

import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;

public class CalculatorServer {


public static void main(String[] args) {
try {
LocateRegistry.createRegistry(1099); // Start RMI registry on
port 1099
Calculator calc = new CalculatorImpl();
Naming.rebind("rmi://localhost:1099/CalculatorService", calc);
System.out.println("Calculator Service is ready.");
} catch (Exception e) {
System.out.println("Server failed: " + e);
}
}
}

Step 4: Create the Client

Create a file CalculatorClient.java to access the remote methods.

import java.rmi.Naming;

public class CalculatorClient {


public static void main(String[] args) {
try {
Calculator calc = (Calculator)
Naming.lookup("rmi://localhost:1099/CalculatorService");

System.out.println("Addition: " + calc.add(10, 5));


System.out.println("Subtraction: " + calc.subtract(10, 5));
System.out.println("Multiplication: " + calc.multiply(10, 5));
System.out.println("Division: " + calc.divide(10, 5));
} catch (Exception e) {
System.out.println("Client failed: " + e);
}
}
}
Step 5: Run the RMI Program

1. Compile all files:

javac *.java

2. Start the RMI registry:

rmiregistry

3. Start the server:

java CalculatorServer

4. Run the client in a separate terminal:

java CalculatorClient

You might also like