0% found this document useful (0 votes)
10 views7 pages

Exp2 Sahil

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Exp2 Sahil

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Experiment 2

Aim: Client Server Implementation using RPC/RMI.

Pre-requisites: Inter-process Communication, Java.

Theory:
 Explain the working and architecture of following with diagram.

1. Remote Procedure Call (RPC)

Overview: Remote Procedure Call (RPC) is a protocol that enables a program to execute a
procedure (or subroutine) on a different address space (usually on a different physical
machine) as if it were a local procedure call. It abstracts the complexity of network
communication and makes remote calls look like local ones. The process of invoking remote
functions or procedures, handling parameters, and dealing with communication between client
and server is managed by the RPC system.

1.1Architecture of RPC:

The architecture of RPC involves the following key components:

1. Client: The client application that invokes a procedure on a remote server.


2. Client Stub: This is the local representation of the remote procedure. It is responsible
for packaging (marshalling) the parameters and calling the remote server.
3. Server Stub: On the server side, the server stub unpacks the parameters
(unmarshalling), invokes the actual procedure, and sends the result back to the client.
4. Server: The server contains the actual procedure or service being called remotely.

In RPC, the procedure call from the client is intercepted by a client stub. This stub packages
the arguments into a message (marshalling) and sends it over the network to the server. The
server then uses a server stub to unpack the message and invoke the procedure. The result is
sent back to the client in a similar manner.
Diagram of RPC Architecture:

1.2. Working of Remote Procedure Call (RPC)

Overview:
RPC is a protocol that allows a program to call a procedure or function on a remote server as
if it were a local procedure call. The complexity of network communication is abstracted, so
the developer doesn’t need to manage network connections, message passing, or other
complexities manually.

Working of RPC:

Here’s how an RPC works step by step:

1. Client Initiates Call:


o The client program calls a function as though it were a local function call. However,
this function is located on a remote server.
o The function call is intercepted by the client stub, which is a local proxy representing
the remote function.

2. Client Stub - Marshaling:


o The client stub packages the parameters (data) of the function into a message format.
This process is called marshaling.
o Marshaling includes converting the function arguments into a byte stream (or some
other portable format) that can be sent over the network. The parameters could be
simple data types (like integers) or complex data structures (arrays, structures).

3. Network Communication:
o The marshaled message (with function arguments) is sent over the network to the
remote server.
o RPC uses standard communication protocols like TCP/IP or UDP to transfer data
across the network.

4. Server Stub - Unmarshaling:


o Once the server receives the message, the server stub unpacks the message (this is
called unmarshaling).
o The server stub converts the byte stream or message back into the original parameters
that the remote procedure expects.

5. Server Executes the Procedure:


o The server stub invokes the actual procedure on the server-side, passing the
unmarshaled parameters to the function as if it were a local function call.
o The procedure executes on the server, processes the data, and generates a result.

6. Server Stub - Returning the Result:


o After the procedure execution, the server stub packages the result into a message
(marshaling) and sends it back over the network to the client.
o The return value could be a simple data type (like an integer) or a complex object.

7. Client Stub - Unmarshaling the Result:


o The client stub receives the result from the server, unmarshals it, and returns the
result to the client application.

8. Client Receives the Result:


o The client application receives the result as though it was obtained from a local
function call, although the procedure was executed remotely.

Example:

 A client wants to get the balance from a bank server. It calls the getBalance() function, which is
located on the remote server. The client does not know how the server processes the request; it
only knows how to call the function. The server processes the request and sends back the
result, such as balance = 1000.

1.3 Steps in RPC:

1. Client Call: The client invokes a local function (stub) as if it were a normal function call.
2. Marshalling: The client stub converts the function parameters into a message format that can
be transmitted over the network.
3. Network Transmission: The marshalled message is sent over the network to the server using
a transport protocol (like TCP or UDP).
4. Server Processing: The server receives the request. The server stub unpacks the message,
extracts the parameters, and calls the corresponding procedure on the server.
5. Execution of the Procedure: The procedure executes on the server side and generates a
result.
6. Return the Result: The server stub packages the result and sends it back to the client via the
network.
7. Unmarshalling and Client Response: The client stub unpacks the result, and the client
application receives the response.

2.Remote Method Invocation (RMI)

Overview: Remote Method Invocation (RMI) is a Java-specific API that enables objects to
invoke methods on remote objects as if they were local. RMI is based on object-oriented
principles, meaning it works with objects and methods rather than procedures or functions.
Unlike RPC, which works at the function level, RMI operates at the object and method level,
making it more suitable for distributed object-oriented systems. It is primarily used in Java
applications to enable communication between remote objects.

2.1 Architecture of RMI:

The architecture of RMI involves these key components:

1. Client: The client application, which needs to invoke methods on remote objects.
2. Remote Interface: The interface that defines the methods that can be called remotely. It
extends java.rmi.Remote and declares the methods that can be invoked remotely.
3. Remote Object: A Java object that implements the remote interface and performs the actual
operations.
4. Stub: A proxy object on the client side that represents the remote object. It acts as the
middleman, forwarding method calls to the server.
5. Skeleton: On the server side, the skeleton receives requests from the client, invokes the actual
remote object method, and sends the result back to the client.
6. RMI Registry: A centralized directory service where remote objects are registered so that
clients can locate them.

Diagram of RMI Architecture:


2.2 Working of Remote Method Invocation (RMI)

Overview: Remote Method Invocation (RMI) is a Java-based mechanism that allows one
Java program (a client) to invoke methods on an object located on a different machine (the
server). RMI provides the ability to invoke methods on remote objects as if they were local to
the client. This enables the creation of distributed applications in Java. Unlike traditional
Remote Procedure Calls (RPC), which are function-oriented, RMI is object-oriented and
operates on methods of objects that are accessible remotely.

Key Components in RMI:

Before understanding how RMI works, let's first look at the key components involved in an
RMI-based system:
1. Remote Interface: This is the interface that defines the methods that can be invoked
remotely. A remote interface extends java.rmi.Remote and declares the methods that
throw java.rmi.RemoteException.
2. Remote Object: A remote object is an object that implements the remote interface.
This is the object whose methods can be called remotely. It contains the actual
business logic for the methods defined in the remote interface.
3. Stub: The stub is a client-side proxy object that represents the remote object. When
the client calls a method on the stub, it forwards the call to the remote object on the
server. The stub handles the low-level details of remote communication, such as
establishing network connections and serializing the method parameters.
4. Skeleton: The skeleton is the server-side counterpart of the stub. It receives method
calls from the stub, unpacks the parameters, and invokes the actual remote method on
the remote object. The skeleton then sends the result back to the client via the stub.
5. RMI Registry: The RMI registry is a directory service that allows clients to look up
and find remote objects. Remote objects are bound to a name in the registry so that
clients can find them. The registry typically runs on the server machine.
6. RMI Runtime: The RMI runtime manages the communication between the client and
the server. It handles the marshalling and unmarshalling of method parameters,
network communication, and exception handling.

2.3 Steps in RMI:

1. Define the Remote Interface: The server defines a remote interface that extends
java.rmi.Remote. This interface contains the method signatures that can be invoked
remotely.
2. Implement the Remote Object: The server creates a class that implements the remote
interface. This class contains the actual logic for the remote method.
3. Export the Remote Object: The remote object is exported so that it can accept
remote method invocations.
4. Bind Remote Object in RMI Registry: The remote object is bound to the RMI
registry using Naming.rebind(). This step allows clients to look up the remote object.
5. Client Lookup: The client looks up the remote object in the RMI registry using
Naming.lookup(), obtaining a reference to the remote object.
6. Method Invocation: The client calls the method on the remote object. The RMI
system uses the stub to forward the method call to the server.
7. Execution on Server: The server executes the method and sends the result back to the
client through the stub and skeleton.
8. Return the Result: The client receives the result as though the method was invoked
locally.

Implementation

Steps:

 On the server machine:


1. Compile ICalculator.java, Calculator.java, and CalculatorServer.java
2. Create the stub and skeleton classes using the command rmic Calculator
3. Copy the Calculator_Stub.class to the client site
4. Start the registry using the command rmiregistry&
5. Start CalculatorServer using java CalculatorServer serverHostname
 On the client machine:
1. Compile ICalculator.java and CalculatorClient.java
2. Run CalculatorClient using the command java CalculatorClient serverHostname
Output
Student are expected to attach the print out of above code with proper output and explanation.
Post-experiment Question :

i. List down the difference between RPC and RMI with example.

Conclusion:

 Summary of Experiment
 Importance of Experiment
 Application of ExperimentJ

You might also like