0% found this document useful (0 votes)
5 views9 pages

DC Exp1

The document outlines an experiment focused on studying and implementing Remote Procedure Call (RPC) in distributed systems, highlighting its importance in simplifying communication, enhancing modularity, and facilitating distributed computing. It details the architecture of RPC, including client-server components, stubs, marshalling, and types of RPC such as synchronous and asynchronous. Additionally, it provides code examples for one-to-one and one-to-many communication scenarios, concluding with the benefits of RPC in executing procedures on remote machines.

Uploaded by

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

DC Exp1

The document outlines an experiment focused on studying and implementing Remote Procedure Call (RPC) in distributed systems, highlighting its importance in simplifying communication, enhancing modularity, and facilitating distributed computing. It details the architecture of RPC, including client-server components, stubs, marshalling, and types of RPC such as synchronous and asynchronous. Additionally, it provides code examples for one-to-one and one-to-many communication scenarios, concluding with the benefits of RPC in executing procedures on remote machines.

Uploaded by

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

Department of Computer Engineering

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())) {

String methodName = (String) in.readObject();


int a = in.readInt();
int b = in.readInt();
Department of Computer Engineering
CalculatorImpl calc = new CalculatorImpl();
int result = 0;
if ("add".equals(methodName)) {
result = calc.add(a, b);
}
out.writeInt(result);
out.flush();
}}
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}}}

Client.java:
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class Client {


public static void main(String[] args) {
try (Socket socket = new Socket("localhost", 1099); // Replace with the server's IP address
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream())) {
out.writeObject("add");
out.writeInt(5);
out.writeInt(3);
out.flush(); int result = in.readInt();
System.out.println("Result: " + result);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();}}}

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:

(B) One to Many


MessageServer.java:
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.List;
Department of Computer Engineering
public class MessageServer extends UnicastRemoteObject implements MessageService {
private List<ClientInterface> clients;
// Constructor
public MessageServer() throws RemoteException {
super();
clients = new ArrayList<>();
}
// Register a client with the server
@Override
public void registerClient(ClientInterface client) throws RemoteException {
clients.add(client);
System.out.println("New client registered.");
}
// Broadcast a message to all registered clients
@Override
public void broadcastMessage(String message) throws RemoteException {
System.out.println("Broadcasting message: " + message);
for (ClientInterface client : clients) {
try {
client.receiveMessage(message);
} catch (RemoteException e) {
System.out.println("Error sending message to client: " + e.getMessage());}} }
// Start the RMI Registry and bind the server
public static void main(String[] args) {
try {
// Create and start the RMI registry
Registry registry = LocateRegistry.createRegistry(2000); // Using port 2000;
// Instantiate the server
MessageServer server = new MessageServer();
// Bind the server to the RMI registry
registry.rebind("MessageService", server);
System.out.println("Server is running...");

// Start broadcasting a message every 5 seconds for demonstration purposes


while (true) {
Thread.sleep(5000);
server.broadcastMessage("Hello from the server!");
}} catch (Exception e) {
Department of Computer Engineering
e.printStackTrace();}}}

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.

You might also like