You are on page 1of 4

22IT012

Ex No 5 Distributed Application Development using Remote Method Invocation


08.03.2024

Algorithm:

1. ARPServer.java:

- Define the `main` method.

- Create an instance of the remote object `StringGeneratorRemote`.

- Bind the remote object to the RMI registry with the name "mike".

- Print a message if successful.

2. StringGenerator.java:

- Define the remote interface `StringGenerator`.

- Declare the remote method ‘generateString()’.

3. ARPClient.java:

- Define the `main` method.

- Look up the remote object with the name "mike" from the RMI registry.

- Call the `generateString()` method multiple times to get different results.

4. StringGeneratorRemote.java:

- Import necessary packages.

- Implement the `generateString()` remote interface.

- Create three separate array of words.

- Using Java’s random library, combine the words to turn it into a phrase.

- Return the result as a String.


22IT012

Coding:

ARPServer.java

import java.rmi.*;

public class ARPServer {

public static void main(String args[]) {

try {

StringGenerator stub = new StringGeneratorRemote();

Naming.rebind("rmi://localhost:5000/mike", stub);

System.out.println("Server started on port 5000");

} catch (Exception e) {

System.out.println(e);

StringGenerator.java

import java.rmi.*;

public interface StringGenerator extends Remote {

public String generateString() throws RemoteException;

StringGeneratorRemote.java

import java.rmi.*;

import java.rmi.server.*;

public class StringGeneratorRemote extends UnicastRemoteObject implements StringGenerator


{

StringGeneratorRemote() throws RemoteException {

super();
22IT012

public String generateString() {

String[] wordListOne = { "cybernetic", "quantum", "luminous", "neural", "transdimensional"


};

String[] wordListTwo = { "hyperloop", "quantum computing", "nanotech", "augmented


reality" };

String[] wordListThree = { "singularity", "metaverse", "holodeck", "nebula", "nanobot" };

int oneLength = wordListOne.length;

int twoLength = wordListTwo.length;

int threeLength = wordListThree.length;

java.util.Random randomGenerator = new java.util.Random();

int rand1 = randomGenerator.nextInt(oneLength);

int rand2 = randomGenerator.nextInt(twoLength);

int rand3 = randomGenerator.nextInt(threeLength);

String phrase = wordListOne[rand1] + " " + wordListTwo[rand2] + " " +


wordListThree[rand3];

return "Enter into the realm of " + phrase + "!";

ARPClient.java

import java.rmi.*;

public class ARPClient {

public static void main(String args[]) {

try {

StringGenerator stub = (StringGenerator) Naming.lookup("rmi://localhost:5000/mike");


22IT012

System.out.println(stub.generateString());

} catch (Exception e) {

System.out.println("Exception: " + e) } }}

Output:

Rubric for Evaluation

Parameter Max Marks Marks Obtained


Complexity of the Application 10
Originality of Code 5
Viva 5
Sub Total 20
Completion of experiment on time 5
Documentation 5
Sub Total 10
Signature of the faculty with Date

Result:
Thus, RMI for the taken application has been done successfully.

You might also like