You are on page 1of 4

DEPARTMENT OF COMPUTER ENGINEERING

Experiment No.05
Semester
B.E. Semester VIII Computer Engineering
Subject
Distributed Computing
Subject Professor
Prof. Amit Nerurkar
In-charge
Assisting Teachers Prof. Amit Nerurkar
Laboratory
Lab No.: M313B (computer lab)
Student Name
Pritish J Kamath
Roll Number
11-217
Grade and Subject
Teachers Signature

Experimen
t Number
Experimen
t Title
Resources
/
Apparatus
Required

05

Objectives
(Skill Set /
Knowledge
Tested /
Imparted)
Theory of
Operation

To implement Remote Method Invocation (RMI) technology and


understand how objects behave, how and when exceptions can occur,
how memory is managed, and how parameters are passed to, and
returned from, remote methods.

Implementing RMI.
Hardware:
IBM PC Compatible Computer
System

Software:
Java

CalculatorServer.java
import java.rmi.*;
import java.rmi.registry.*;
public class CalculatorServer{
public static void main(String args[]){
try{
CalculatorInterface stub=new CalculatorRemote();
Naming.rebind("rmi://localhost:5000/calculator",stub);
} catch(Exception e) {
System.out.println(e);
}
}
}

CalculatorClient.java
import java.rmi.*;
import java.util.Scanner;
public class CalculatorClient{
public static void main(String args[]){
try{
CalculatorInterface
stub=(CalculatorInterface)Naming.lookup("rmi://localhost:5000/calculat
or");
//System.out.println(stub.add(34,4));
Scanner sc = new Scanner(System.in);
int ch = 0;
do {
printMenu();
ch = sc.nextInt();
sc.nextLine();
switch(ch) {
case 0: break;
case 1:
System.out.print("enter two
numbers: ");
System.out.println("sum = " +
stub.add(sc.nextDouble(), sc.nextDouble()));
sc.nextLine();
break;
case 2:
System.out.print("enter two
numbers: ");
System.out.println("sub = " +
stub.sub(sc.nextDouble(), sc.nextDouble()));
sc.nextLine();
break;
case 3:
System.out.print("enter two
numbers: ");
System.out.println("mul = " +
stub.mul(sc.nextDouble(), sc.nextDouble()));
sc.nextLine();
break;
case 4:
System.out.print("enter two
numbers: ");
System.out.println("div = " +
stub.div(sc.nextDouble(), sc.nextDouble()));
sc.nextLine();
break;
default: System.out.println("invalid input");

break;
}
} while (ch!=0);
} catch(Exception e) {}
}
private static void printMenu() {
System.out.println("Enter
choice:\n1:Add\n2:Subtract\n3:Multiply\n4:Divide\n0:Exit\n");
}
}
CalculatorRemote.java
import java.rmi.*;
import java.rmi.server.*;
public class CalculatorRemote extends UnicastRemoteObject
implements CalculatorInterface {
CalculatorRemote()throws RemoteException {
super();
}
public double add(double x,double y) {
return x+y;
}
public double sub(double x,double y) {
return x-y;
}
public double mul(double x,double y) {
return x*y;
}
public double div(double x,double y) {
return x/y;
}
}

CalculatorInterface.java
import java.rmi.*;
public interface CalculatorInterface extends Remote{
public double add(double x, double y)throws RemoteException;
public double sub(double x, double y)throws RemoteException;
public double mul(double x, double y)throws RemoteException;
public double div(double x, double y)throws RemoteException;
}

Output

Conclusion

A primary goal for the RMI designers was to allow programmers to


develop distributed Java programs with the same syntax and
semantics used for non-distributed programs. To do this, they had to
carefully map how Java classes and objects work in a single
Java Virtual Machine (JVM) to a new model of how classes and
objects would work in a distributed (multiple JVM) computing
environment.

You might also like