Home Java Network RMI Client And RMI Server Implementation
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

RMI Client And RMI Server Implementation

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

public interface ReceiveMessageInterface extends Remote{
  void receiveMessage(String x) throws RemoteException;
}

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 xthrows 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.

Download of this code.

Output of the above program:

C:\rose>javac RmiServer.java
C:\rose>java RmiServer 
this address=roseindi/192.168.10.104,port=3232
t=
_3232

Here is the code of 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.

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


Download of this Code

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 17, 2009

Ask Questions?    Discuss: RMI Client And RMI Server Implementation   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
hinal prajapati
March 20, 2012
rmi

count how many words,letters,vovels,whitespace are there in the given string usin rmi progarmme?
Amit Rai
October 18, 2012
Hii

Its nice and very simple code.
ahmedin bergena
December 23, 2012
java

it is very intersting code continue
DMCA.com