0% found this document useful (0 votes)
639 views6 pages

A) Write Fully Working Remote Procedure Call (RPC) Program Using Java. Example

The document provides an example of implementing a fully working remote procedure call (RPC) program in Java. It defines the interface for the RPC calls, implements a server class, and implements a client class. The interface defines a getName method. The server implements this interface and returns hardcoded names for different user IDs. The client looks up the server stub, calls the getName method, and prints the returned names.

Uploaded by

Geleta Mitiku
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)
639 views6 pages

A) Write Fully Working Remote Procedure Call (RPC) Program Using Java. Example

The document provides an example of implementing a fully working remote procedure call (RPC) program in Java. It defines the interface for the RPC calls, implements a server class, and implements a client class. The interface defines a getName method. The server implements this interface and returns hardcoded names for different user IDs. The client looks up the server stub, calls the getName method, and prints the returned names.

Uploaded by

Geleta Mitiku
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/ 6

A) Write fully working remote procedure call (RPC) program using java.

Example

Here is an example of a fully working RPC program using Java:

First, we need to define the interfaces for the RPC calls (in two separate files):

UserService.java:
```
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface UserService extends Remote {


public String getName(String userId) throws RemoteException;
}
```

UserServiceImpl.java:
```
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class UserServiceImpl extends UnicastRemoteObject implements UserService


{
public UserServiceImpl() throws RemoteException {
super();
}

@Override
public String getName(String userId) throws RemoteException {
if (userId.equals("1")) {
return "John Doe";
} else if (userId.equals("2")) {
return "Jane Smith";
} else {
return "Unknown user";
}
}
}
```

Next, we implement the server (in one file):

UserServer.java:
```
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class UserServer {


public static void main(String[] args) {
try {
UserService userService = new UserServiceImpl();
UserService stub = (UserService) UnicastRemoteObject.exportObject(userService, 0);

Registry registry = LocateRegistry.getRegistry();


registry.bind("UserService", stub);

System.out.println("UserServer ready.");
} catch (Exception e) {
System.err.println("UserServer exception: " + e.toString());
e.printStackTrace();
}
}
}
```

Finally, we implement the client (in one file):


UserClient.java:
```
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class UserClient {


public static void main(String[] args) {
try {
Registry registry = LocateRegistry.getRegistry("localhost");
UserService userService = (UserService) registry.lookup("UserService");

String name = userService.getName("1");


System.out.println("Name for user 1: " + name);

name = userService.getName("2");
System.out.println("Name for user 2: " + name);

name = userService.getName("3");
System.out.println("Name for user 3: " + name);
} catch (Exception e) {
System.err.println("UserClient exception: " + e.toString());
e.printStackTrace();
}
}
}
```

To run this program, we need to compile all the files, start the server (`java
UserServer`) and then start the client (`java UserClient`). The client should output:

```
Name for user 1: John Doe
Name for user 2: Jane Smith
Name for user 3: Unknown user
```
B) Write fully working remote method invocation (RMI) program using java.
Example
Here is an example of a fully working RMI program in Java:

Server side implementation:

```java
import java.rmi.*;
import java.rmi.server.*;

public class RMIImplementation extends UnicastRemoteObject implements RMIInterface {

public RMIImplementation() throws RemoteException {


super();
}

public int add(int x, int y) throws RemoteException {


return x + y;
}

}
```

Client side implementation:

```java
import java.rmi.*;

public class RMIClient {

public static void main(String[] args) throws Exception {


RMIInterface obj = (RMIInterface) Naming.lookup("//localhost/RMIServer");
int result = obj.add(10, 20);
System.out.println("Result: " + result);
}

}
```

Interface implementation:

```java
import java.rmi.*;

public interface RMIInterface extends Remote {


public int add(int x, int y) throws RemoteException;
}
```

Server startup code:

```java
import java.rmi.*;

public class RMIServer {

public static void main(String[] args) throws Exception {


RMIImplementation obj = new RMIImplementation();
Naming.rebind("//localhost/RMIServer", obj);
}

}
```

To run the program, first run the server startup code:

```
java RMIServer
```

Then run the client code:


```
java RMIClient
```

The output should be:

```
Result: 30
```

You might also like