CN Exp 5
CN Exp 5
Ex No 5
Distributed Application Development using Remote Method Invocation
Date:
10 Mar 2025
AIM:
THEORY:
RMI
RMI allows a Java program to call methods on an object located on another
machine (remotely), as if it were a local object.
It helps in distributed computing, where different parts of a program can run on
different systems.
Difference between RMI and RPC
Stubs
A stub is a client-side proxy that represents the remote object.
When a client calls a method on a remote object, the stub forwards the request
to the actual object on the server.
How It Works:
1. The client calls a method on the stub.
2. The stub sends the request over the network.
3. The server executes the actual method and sends back the result.
Skeletons
A skeleton is a server-side proxy that receives client requests and passes them
to the actual remote object.
It helps in handling communication between the stub and the remote
object.
How It Works:
1. The stub sends the request to the server.
2. The skeleton receives the request and calls the actual method.
3. The result is sent back to the stub.
Marshalling / Unmarahalling
Marshalling = Converting an object into a format (like bytes) to send over the
network.
Unmarshalling = Converting the received bytes back into an object.
TWO-SUM:
ALGORITHM:
1. Create an empty HashMap (map)
2. Loop through array 'nums' with index 'i':
a. Compute complement = target - nums[i]
b. If complement exists in map:
- Return indices {map[complement], i}
c. Store nums[i] in the map with its index
3. If no pair found, return {-1, -1}
CODING:
@Override
public int[] twoSum(int[] nums, int target) throws RemoteException {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[]{map.get(complement), i};
}
map.put(nums[i], i);
}
return new int[]{-1, -1}; // No valid pair found
}
}
}
}
4. Client Code (TwoSumClient.java)
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Arrays;
OUTPUT:
RESULT:
Thus, the distributed application has been developed using Remote Method Invocation.