ServerIntf.java
import java.rmi.*;
import java.rmi.server.*;
public interface ServerIntf extends Remote {
public double sum(double... numbers) throws RemoteException;
public double average(double... numbers) throws RemoteException;
}
ServerImpl.java
import java.rmi.*;
import java.rmi.server.*;
public class ServerImpl extends UnicastRemoteObject implements ServerIntf {
public ServerImpl() throws RemoteException {
}
public double sum(double... numbers) throws RemoteException {
double result = 0;
for(double num : numbers) {
result += num;
}
return result;
}
public double average(double... numbers) throws RemoteException {
double result = 0;
int n = numbers.length;
for(double num : numbers) {
result += num;
}
return result/n;
}
}
Server.java
import java.rmi.*;
import java.rmi.server.*;
public class Server {
public static void main(String[] args) {
try {
ServerImpl serverImpl = new ServerImpl();
Naming.rebind("Server", serverImpl);
} catch(Exception exp) {
exp.printStackTrace();
}
}
}
Client.java
import java.rmi.*;
import java.rmi.server.*;
import java.io.*;
public class Client {
public static void main(String[] args) {
try {
String serverURL = "rmi://" + args[0] + "/Server";
ServerIntf serverIntf = (ServerIntf)Naming.lookup(serverURL);
BufferedReader buffRdr = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the limit...");
int n = Integer.parseInt(buffRdr.readLine());
System.out.println("Enter the numbers...");
double[] numbers = new double[n];
for(int i = 0 ; i < n ; i++) {
numbers[i] = Double.parseDouble(buffRdr.readLine());
}
System.out.println("Sum..." + serverIntf.sum(numbers));
System.out.println("Average..." + serverIntf.average(numbers));
} catch(Exception exp) {
exp.printStackTrace();
}
}
}
c:\rmi\server> java Server
c:\rmi\client> java Client 127.0.0.1
NB: Refer the previous RMI programs to get an idea about how to run the program.
0 Comments