The following program demonstrates How to Create a Client-Server Application Using Java RMI.

In order to demonstrate a client-server application using Java RMI (Remote Method Invocation), we’ll create a simple example where the client sends a message to the server, and the server responds with a modified version of that message. The following are the steps to create this Java RMI application.

Define the Remote Interface

At first, define a remote interface that declares the remote methods that the client can call on the server. This interface should extend java.rmi.Remote, and each method should throw java.rmi.RemoteException.

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface RMIServerInterface extends Remote {
    String echo(String message) throws RemoteException;
}

Implement the Server

Next, implement the server class that provides the remote methods defined in the interface. The server class should extend java.rmi.server.UnicastRemoteObject and implement the interface.

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class RMIServer extends UnicastRemoteObject implements RMIServerInterface {
    public RMIServer() throws RemoteException {
        // Constructor required to throw RemoteException
    }

    @Override
    public String echo(String message) throws RemoteException {
        return "Server Response: " + message;
    }

    public static void main(String[] args) {
        try {
            // Create and bind the server object to the RMI registry
            RMIServer server = new RMIServer();
            java.rmi.registry.LocateRegistry.createRegistry(1099);
            java.rmi.Naming.rebind("RMIServer", server);

            System.out.println("Server is running...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Implement the Client

Create a client program that looks up the server object in the RMI registry and invokes the remote methods.

import java.rmi.Naming;

public class RMIClient {
    public static void main(String[] args) {
        try {
            // Lookup the server object in the RMI registry
            RMIServerInterface server = (RMIServerInterface) Naming.lookup("rmi://localhost/RMIServer");

            // Invoke the remote method
            String message = "Hello, RMI Server!";
            String response = server.echo(message);

            System.out.println("Client Response: " + response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Compile and Run

Compile both the server and client classes. Make sure the RMI registry is running on the default port 1099.

Start the Server

Now you can start the server by running the RMIServer class.

Start the Client

Similarly, run the RMIClient class, which will invoke the remote method on the server, and display the response.

This is a basic example of a Java RMI client-server application. The client invokes the echo method on the server, which returns a modified message.


Further Reading

Spring Framework Practice Problems and Their Solutions

From Google to the World: The Story of Go Programming Language

Why Go? Understanding the Advantages of this Emerging Language

Creating and Executing Simple Programs in Go

20+ Interview Questions on Go Programming Language

Java Practice Exercise

programmingempire

Princites