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

Rmi

This document describes how to build a simple client-server application using Java RMI (Remote Method Invocation). It defines a remote Calculator interface with arithmetic methods, implements this interface, runs the implementation as a server, and writes a client that looks up and invokes operations on the remote object. The client and server are run separately after compiling all code, allowing for remote calls and distributed computation.

Uploaded by

l2845610
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)
78 views3 pages

Rmi

This document describes how to build a simple client-server application using Java RMI (Remote Method Invocation). It defines a remote Calculator interface with arithmetic methods, implements this interface, runs the implementation as a server, and writes a client that looks up and invokes operations on the remote object. The client and server are run separately after compiling all code, allowing for remote calls and distributed computation.

Uploaded by

l2845610
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

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.

You might also like