Certainly!
Below is an example of a simple client-server application using RMI (Remote Method
Invocation) in Java.
**1. Define the Remote Interface (`Calculator.java`):**
```java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Calculator extends Remote {
int add(int a, int b) throws RemoteException;
int subtract(int a, int b) throws RemoteException;
int multiply(int a, int b) throws RemoteException;
int divide(int a, int b) throws RemoteException;
}
```
**2. Implement the Remote Interface (`CalculatorImpl.java`):**
```java
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class CalculatorImpl extends UnicastRemoteObject implements Calculator {
protected CalculatorImpl() throws RemoteException {
super();
}
@Override
public int add(int a, int b) throws RemoteException {
return a + b;
}
@Override
public int subtract(int a, int b) throws RemoteException {
return a - b;
}
@Override
public int multiply(int a, int b) throws RemoteException {
return a * b;
}
@Override
public int divide(int a, int b) throws RemoteException {
if (b != 0) {
return a / b;
} else {
throw new ArithmeticException("Cannot divide by zero");
}
}
}
```
**3. Create the Server (`Server.java`):**
```java
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
public class Server {
public static void main(String[] args) {
try {
Calculator calculator = new CalculatorImpl();
// Create and export a registry on the default port
LocateRegistry.createRegistry(1099);
// Bind the remote object to the registry with the name "CalculatorService"
Naming.rebind("CalculatorService", calculator);
System.out.println("Server is ready.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
**4. Create the Client (`Client.java`):**
```java
import java.rmi.Naming;
public class Client {
public static void main(String[] args) {
try {
// Lookup the remote object by the name "CalculatorService"
Calculator calculator = (Calculator) Naming.lookup("rmi://localhost/CalculatorService");
// Call remote methods
int resultAdd = calculator.add(10, 5);
System.out.println("Result of addition: " + resultAdd);
int resultSubtract = calculator.subtract(10, 5);
System.out.println("Result of subtraction: " + resultSubtract);
int resultMultiply = calculator.multiply(10, 5);
System.out.println("Result of multiplication: " + resultMultiply);
int resultDivide = calculator.divide(10, 5);
System.out.println("Result of division: " + resultDivide);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
**5. Compile and Run:**
- Compile all Java files (`Calculator.java`, `CalculatorImpl.java`, `Server.java`, `Client.java`).
- Start the RMI registry on the server side: `rmiregistry`.
- Run the `Server` program on the server side.
- Run the `Client` program on the client side.
This example demonstrates a simple calculator application using RMI. The server exposes a
`Calculator` interface, and the client invokes remote methods on that interface. Make sure to
handle exceptions properly and consider security aspects when using RMI in real-world
applications.