0% found this document useful (0 votes)
32 views8 pages

Advance Java Experiment 05

Uploaded by

m.sahil2604a
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)
32 views8 pages

Advance Java Experiment 05

Uploaded by

m.sahil2604a
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

Poornima College of Engineering, Jaipur

Department of Computer Engineering


Advance Java Lab (5CS4-24)
[Link] III Year, V Semester
Experiment – 05
_____________________________________________________________________

Objective: Create a simple calculator application that demonstrates the use of RMI. You are not required to create
GUI.

We have to run these four classes to make calculator using RMI.


o [Link]
o [Link]
o [Link]
o [Link]

[Link]

public interface Calculator


extends [Link] {
public long add(long a, long b)
throws [Link];

public long sub(long a, long b) throws


[Link];

public long mul(long a, long b) throws


[Link];

public long div(long a, long b)


throws [Link];
}
[Link]

public class CalculatorImpl extends


[Link]
implements Calculator {

// Implementations must have an


//explicit constructor
// in order to declare the
//RemoteException exception

public CalculatorImpl()
throws [Link] {
super();
}

public long add(long a, long b)


throws [Link] {
return a + b;
}

public long sub(long a, long b)


throws [Link] {
return a - b;
}

public long mul(long a, long b)


throws [Link] {
return a * b;
}

public long div(long a, long b)


throws [Link] {
return a / b;
}}

[Link]

import [Link];
import [Link];
import [Link];
import [Link];

public class CalculatorClient {

public static void main(String[] args) {


try {
Calculator c = (Calculator)
[Link](
"rmi://localhost/CalculatorService");
[Link]( [Link](4, 3) );
[Link]( [Link](4, 5) );
[Link]( [Link](3, 6) );
[Link]( [Link](9, 3) );
}
catch (MalformedURLException murle) {
[Link]();
[Link](
"MalformedURLException");
[Link](murle);
}
catch (RemoteException re) {
[Link]();
[Link](
"RemoteException");
[Link](re);
}
catch (NotBoundException nbe) {
[Link]();
[Link](
"NotBoundException");
[Link](nbe);
}
catch (
[Link]
ae) {
[Link]();
[Link](
"[Link]");
[Link](ae);}}
[Link]

import [Link];

public class CalculatorServer {

public CalculatorServer() {
try {
Calculator c = new CalculatorImpl();
[Link]("rmi://localhost:1099/CalculatorService", c);
} catch (Exception e) {
[Link]("Trouble: " + e);
}
}

public static void main(String args[]) {


new CalculatorServer();
}
}

//Now use rmic to create the stub and skeleton class files.

> rmic CalculatorImpl

Running the RMI System


You are now ready to run the system! You need to start three consoles, one for the server, one for the
client, and one for the RMIRegistry.
o Start with the Registry. You must be in the directory that contains the classes you have
written. From there, enter the following:

> rmiregistry
o If all goes well, the registry will start running and you can switch to the next console.
In the second console start the server hosting the CalculatorService, and enter the
following:

> java CalculatorServer


o It will start, load the implementation into memory and wait for a client connection.
In the last console, start the client program.

> java CalculatorClient//

OUTPUT –

1
9
18
Poornima College of Engineering, Jaipur

You might also like