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

RMI

The document defines a remote method invocation (RMI) based distributed calculator application with client and server components. The server component starts an RMI registry, publishes a CalculatorImpl object, and waits for requests. The client component looks up the published calculator service, prompts the user for input, calls the appropriate operation based on user selection, and displays the result. The CalculatorImpl class implements the remote CalculatorInterface to handle requests.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views3 pages

RMI

The document defines a remote method invocation (RMI) based distributed calculator application with client and server components. The server component starts an RMI registry, publishes a CalculatorImpl object, and waits for requests. The client component looks up the published calculator service, prompts the user for input, calls the appropriate operation based on user selection, and displays the result. The CalculatorImpl class implements the remote CalculatorInterface to handle requests.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package server;

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

import methode.CalculatorImpl;
import methode.CalculatorInterface;

public class CalculatorServer {


public static void main(String[] args) {
try {
// Start the RMI registry on default port (1099)
LocateRegistry.createRegistry(1099);

// Create an instance of the CalculatorImpl


CalculatorInterface calculator = new CalculatorImpl();

// Bind the instance to the RMI registry with a specific name


Naming.rebind("rmi://localhost:1099/CalculatorService", calculator);

System.out.println("Calculator Server is ready.");


} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
e.printStackTrace();
}
}
}
*************************************************************************
package client;

import java.rmi.Naming;

import methode.CalculatorInterface;
import java.util.Scanner;

public class CalculatorClient {


public static void main(String[] args) {
try {
// Look up the RMI server object using the specified name

CalculatorInterface calculator = (CalculatorInterface)


Naming.lookup("rmi://localhost:1099/CalculatorService");
Scanner scanner = new Scanner(System.in);
System.out.println("entre a : ");
int a= scanner.nextInt();
System.out.println("entre b : ");
int b= scanner.nextInt();
System.out.println("choose from the following operation To
calculate :");
System.out.println("choose 1 to Add");
System.out.println("choose 2 to defference");
System.out.println("choose 3 to multiply");
System.out.println("choose 4 to divide");
System.out.println("choose 5 to puissance");
int n = scanner.nextInt();
// Use methods on the RMI server object

if(n==1){
int sum = calculator.add(a,b);
System.out.println("Sum: " + sum); }
else if(n==2){
int difference = calculator.subtract(a,b);
System.out.println("Difference: " + difference);
}
else if(n==3){
int product = calculator.multiply(a,b);
System.out.println("Product: " + product);
}
else if(n==4){
double quotient = calculator.divide(a,b);
System.out.println("Quotient: " + quotient);
}
else {
int puissance = calculator.puissance(a, b);
System.out.println("Puissance: " + puissance);
}
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
e.printStackTrace();
}
}
}
*********************************************************************
package methode;

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

public class CalculatorImpl extends UnicastRemoteObject implements


CalculatorInterface {
private static final long serialVersionUID = 1L;

public 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 double divide(int a, int b) throws RemoteException {


if (b == 0) {
throw new RemoteException("Cannot divide by zero.");
}
return (double) a / b;
}
public int puissance(int a, int b) throws RemoteException {
return (int) Math.pow(a, b);
}

}
*******************************************************************
package methode;
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface CalculatorInterface 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;
double divide(int a, int b) throws RemoteException;
int puissance(int a, int b) throws RemoteException;
}

You might also like