RMI Client And RMI Server Implementation in Java

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.*;

public interface ReceiveMessageInterface extends Remote{
void receiveMessage(String x) throws RemoteException;
}
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);
}
}
}
RMI Client:
------------------------------------------------------

import java.rmi.*;
import java.rmi.registry.*;
import java.net.*;

public class RmiClient{
static public void main(String args[]){
ReceiveMessageInterface rmiServer;
Registry registry;
String serverAddress=args[0];
String serverPort=args[1];
String text=args[2];
System.out.println
("sending " + text + " to " +serverAddress + ":" + serverPort);
try{
registry=LocateRegistry.getRegistry
(serverAddress,(new Integer(serverPort)).intValue());
rmiServer=(ReceiveMessageInterface)(registry.lookup("rmiServer"));
// call the remote method
rmiServer.receiveMessage(text);
}
catch(RemoteException e){
e.printStackTrace();
}
catch(NotBoundException e){
System.err.println(e);
}
}
}

lookup(): This is the method that returns a reference, a stub, for the remote object associated with the specified name.

No comments:

Post a Comment