The RMI application comprises of the two separate programs, a server and a client.
RMI Client And RMI Server Implementation
Introduction
The RMI application comprises of the two separate programs, a server and a client. A typical server program
creates some remote objects, makes references to these objects accessible, and
waits for clients to invoke methods on these objects. The RMI application
provides the mechanism by which the server and the client communicate and pass
information back and forth. The RMI distributed application uses the RMI
Registry to obtain a reference to a remote object. The server calls the registry
to associate a name with a remote object. The client looks up the remote object
by its name in the server?s registry and then invokes a method on it.
Program
description:
In this section, you will learn how to send massage from RmiClient to the RmiServer. Here, we are going to
create "ReceiveMessageInterface" interface. The interface
defines the methods that can be invoked from the client. Essentially, the
interface defines the client's view of the remote object. After that, we will
create a class named "RMIServer".
The RMI Server accepts tasks from clients, runs the tasks, and returns any
result. The server code consists of an interface and a class.
In this class, the ?receiveMessage()?
method, which
is called from the remote client, is defined. This class is the implementation
of the RMI interface. The RmiServer creates the ?registry?.
This is a kind of directory. Its
key is a name (which is the ID of a remote object) and its content is an object.
This object is looked up from a remote program by the name. This registry is
accessed from a remote object by the IP address or host name and the port
number.
createRegistry(): This is the method creates and
exports a registry on the local host that accepts requests on the specified
port.
ReceiveMessageInterface.java
import java.rmi.*;
|
The above code defines the RMI interface. The receiveMessage() method is implemented in the server class.
Here is the code of RMI Server:
import java.rmi.*; import java.rmi.registry.*; import java.rmi.server.*; import java.net.*; public class RmiServer extends java.rmi.server.UnicastRemoteObject implements ReceiveMessageInterface{ String address; Registry registry; public void receiveMessage(String x) throws RemoteException{ System.out.println(x); } public RmiServer() throws RemoteException{ try{ address = (InetAddress.getLocalHost()).toString(); } catch(Exception e){ System.out.println("can't get inet address."); } int port=3232; System.out.println("this address=" + address + ",port=" + port); try{ registry = LocateRegistry.createRegistry(port); registry.rebind("rmiServer", this); } catch(RemoteException e){ System.out.println("remote exception"+ e); } } static public void main(String args[]){ try{ RmiServer server = new RmiServer(); } catch (Exception e){ e.printStackTrace(); System.exit(1); } } } |
The above class uses LocateRegistry class to create a remote object registry that accepts calls on a specific port.
Output of the above program:
C:\rose>javac RmiServer.java C:\rose>java RmiServer this address=roseindi/192.168.10.104,port=3232t= _3232 |
Here is the code of RMI Client:
import java.rmi.*;
|
lookup(): This is the method that returns a reference, a stub, for the remote object
associated with the specified name
.
Output of the above program:
C:\rose>java RmiClient 192.168.10.104 3232
roseindia sending roseindia to 192.168.10.104:3232 C:\rose> |
If the RMI client sends any type of massage then massage will be displayed
on the RMI Server.
C:\rose>java RmiServer this address=roseindi/192.168.10.104,port=3232 roseindia |