DC Exp1
DC Exp1
EXPERIMENT NO.: 01
Date: / /2025
Roll No.:
Aim: To study and implement Remote Procedure Call (RPC) in Distributed System.
Theory:
Remote Procedure Call (RPC) is a protocol used in distributed systems that allows a program to
execute a procedure (subroutine) on a remote server or system as if it were a local procedure call.
RPC enables a client to invoke methods on a server residing in a different address space (often on
a different machine) as if they were local procedures. The client and server communicate over a
network, allowing for remote interaction and computation.
Importance of Remote Procedural Call (RPC) in Distributed Systems:
Simplified Communication
Abstraction of Complexity: RPC abstracts the complexity of network communication,
allowing developers to call remote procedures as if they were local, simplifying the
development of distributed applications.
Consistent Interface: Provides a consistent and straightforward interface for invoking
remote services, which helps in maintaining uniformity across different parts of a system.
Enhanced Modularity and Reusability
Decoupling: RPC enables the decoupling of system components, allowing them to interact
without being tightly coupled.
Service Reusability: Remote services or components can be reused across different
applications or systems, enhancing code reuse and reducing redundancy.
Facilitates Distributed Computing
Department of Computer Engineering
Inter-Process Communication (IPC): RPC allows different processes running on separate
machines to communicate and cooperate, making it essential for building distributed
applications that require interaction between various nodes.
Resource Sharing: Enables sharing of resources and services across a network, such as
databases, computation power, or specialized functionalities.
Remote Procedural Call (RPC) Architecture in Distributed Systems:
1. Client and Server Components
Client: The client is the component that makes the RPC request. It invokes a procedure or
method on the remote server by calling a local stub, which then handles the details of
communication.
Server: The server hosts the actual procedure or method that the client wants to execute. It
processes incoming RPC requests and sends back responses.
2. Stubs
Client Stub: Acts as a proxy on the client side. It provides a local interface for the client to
call the remote procedure. The client stub is responsible for marshalling (packing) the
procedure arguments into a format suitable for transmission and for sending the request to
the server.
Server Stub: On the server side, the server stub receives the request, unmarshals (unpacks)
the arguments, and invokes the actual procedure on the server. It then marshals the result
and sends it back to the client stub.
3. Marshalling and Unmarshalling
Marshalling: The process of converting procedure arguments and return values into a
format that can be transmitted over the network. This typically involves serializing the data
into a byte stream.
Unmarshalling: The reverse process of converting the received byte stream back into the
original data format that can be used by the receiving system.
4. RPC Framework
Interface Definition Language (IDL): Used to define the interface for the remote
procedures. IDL specifies the procedures, their parameters, and return types in a language-
neutral way. This allows for cross-language interoperability.
RPC Protocol: Defines how the client and server communicate, including the format of
requests and responses, and how to handle errors and exceptions.
5. Error Handling and Fault Tolerance
Timeouts and Retries: Mechanisms to handle network delays or failures by retrying
requests or handling timeouts gracefully.
Exception Handling: RPC frameworks often include support for handling remote
exceptions and reporting errors back to the client.
Types of Remote Procedural Call (RPC) in Distributed Systems:
Department of Computer Engineering
1. Synchronous RPC
Description: In synchronous RPC, the client sends a request to the server and waits for the
server to process the request and send back a response before continuing execution.
2. Asynchronous RPC
Description: In asynchronous RPC, the client sends a request to the server and continues
its execution without waiting for the server’s response. The server’s response is handled
when it arrives.
3. One-Way RPC
Description: One-way RPC involves sending a request to the server without expecting any
response. It is used when the client does not need a return value or acknowledgment from
the server.
4. Callback RPC
Description: In callback RPC, the client provides a callback function or mechanism to the
server. After processing the request, the server invokes the callback function to return the
result or notify the client.
5. Batch RPC
Description: Batch RPC allows the client to send multiple RPC requests in a single batch
to the server, and the server processes them together.
CODE:
(A) One to One communication
Server.java:
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(1099)) {
System.out.println("Server is ready.");
while (true) {
try (Socket clientSocket = serverSocket.accept();
ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
ObjectOutputStream out = new
ObjectOutputStream(clientSocket.getOutputStream())) {
Client.java:
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
CalculatorImpl.java:
public class CalculatorImpl implements Calculator {
public int add(int a, int b) {
return a + b;}}
Calculator.java:
Department of Computer Engineering
import java.io.Serializable;
public interface Calculator extends Serializable {
int add(int a, int b);}
Output:
MessageService.java:
import java.rmi.Remote;
import java.rmi.RemoteException;
// Define the remote interface
public interface MessageService extends Remote {
// Method to send a broadcast message to all clients
void broadcastMessage(String message) throws RemoteException;
// Method for clients to register themselves with the server
void registerClient(ClientInterface client) throws RemoteException;
}
MessageClient.java:
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class MessageClient extends UnicastRemoteObject implements ClientInterface {
// Constructor
public MessageClient() throws RemoteException {
super();}
// Receive a message from the server
@Override
public void receiveMessage(String message) throws RemoteException {
System.out.println("Received message: " + message);
}
// Start the client and connect to the server
public static void main(String[] args) {
try {
// Locate the RMI registry and look for the server
Registry registry = LocateRegistry.getRegistry("localhost", 2000);
MessageService server = (MessageService) registry.lookup("MessageService");
// Create the client
MessageClient client = new MessageClient();
// Register the client with the server
server.registerClient(client);
System.out.println("Client is registered and listening for messages...");
} catch (Exception e) {
Department of Computer Engineering
e.printStackTrace();}}}
ClientInterface.java:
import java.rmi.Remote;
import java.rmi.RemoteException;
// Client interface to receive messages from the server
public interface ClientInterface extends Remote {
void receiveMessage(String message) throws RemoteException; }
Output:
Department of Computer Engineering
Conclusion:
Implementing Remote Procedure Call (RPC) enables a program to execute a procedure on a
remote machine by abstracting network complexity, ensuring seamless client-server interaction,
secure data serialization, effective error handling, and optimized performance, ultimately
facilitating efficient distributed computing and inter-process communication.