Practical No 3
Practical No 3
(Sem-II) [2023-24]
Practical No: 02
Title: Design a distributed application using RMI for remote computation where client submits
two strings to the server and server returns the concatenation of the given strings.
Objective:
1. Develop a client-server application using Remote Method Invocation (RMI) for remote
computation.
2. Enable the client to submit two strings to the server for concatenation.
3. Ensure that the server concatenates the given strings and returns the result to the client
program.
• Jupyter Notebook, any Java IDE
• A machine with at least 8GB of RAM is recommended for model training.
• A multi-core CPU is suitable, and for faster training, a GPU (Graphics Processing Unit)
is highly recommended.
Prerequisities:
• Basic understanding of Java programming
The RMI (Remote Method Invocation) is an API that provides a mechanism to create
distributed application in java. The RMI allows an object to invoke methods on an object
running in another JVM.
The RMI provides remote communication between the applications using two objects stub and
skeleton.
Understanding stub and skeleton
RMI uses stub and skeleton object for communication with the remote object.
A remote object is an object whose method can be invoked from another JVM. Let's understand
the stub and skeleton objects.
stub
The stub is an object, acts as a gateway for the client side. All the outgoing requests are routed
through it. It resides at the client side and represents the remote object. When the caller invokes
method on the stub object, it does the following tasks:
1. It initiates a connection with remote Virtual Machine (JVM),
2. It writes and transmits (marshals) the parameters to the remote Virtual Machine (JVM)
3. It waits for the result
4. It reads (unmarshals) the return value or exception, and
5. It finally, returns the value to the caller.
skeleton
The skeleton is an object, acts as a gateway for the server side object. All the incoming requests
are routed through it. When the skeleton receives the incoming request, it does the following
tasks:
1. It reads the parameter for the remote method
2. It invokes the method on the actual remote object, and
3. It writes and transmits (marshals) the result to the caller.In the Java 2 SDK, an stub protocol
was introduced that eliminates the need for skeletons.
Next step is to create stub and skeleton objects using the rmi compiler. The rmic tool invokes
the RMI compiler and creates stub and skeleton objects.
1. rmic AdderRemote
4) Start the registry service by the rmiregistry tool
Now start the registry service by using the rmiregistry tool. If you don't specify the port number,
it uses a default port number. In this example, we are using the port number 5000.
rmiregistry 5000
5) Create and run the server application
Now rmi services need to be hosted in a server process. The Naming class provides methods
to get and store the remote object.
In Python, you can use the Pyro4 library to implement a distributed application using RMI
(Remote Method Invocation) for remote computation. Pyro4 is a Python Remote Objects
library that simplifies the process of building distributed applications.
Before you start, you need to install the Pyro4 library. You can install it using:
Server Implementation:
# server.py
import Pyro4
@Pyro4.expose
class StringConcatenationServer:
def concatenate_strings(self, str1, str2):
result = str1 + str2
return result
def main():
daemon.requestLoop()
if __name__ == "__main__":
main()
Client Implementation:
# client.py
import Pyro4
def main():
with open("server_uri.txt", "r") as f:
uri = f.read()
if __name__ == "__main__":
main()
Steps to Run:
Install Pyro4 library
Then Use command Pyro4-ns
And use following steps
1)Save the server code in a file, e.g., server.py
Department of Artificial Intelligence and Data Science, MCOERC, NASHIK
Computer Laboratory-III B.E.(Sem-II) [2023-24]