Home Tutorials Rmi RMI-Example-1
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

RMI-Example-1

Advertisement
RMI-Example-1

RMI-Example-1

     

This is a very simple example of RMI where you will come to know how RMI works and what are the steps of executions. Here listed four java files and steps for executing this application.

Code:

RemoteInterface.java

import java.rmi.*;

public interface RemoteInterface extends Remote
{
  public int add(int x,int y)throws Exception;
}

ServerImplements.java

import java.rmi.*;
import java.rmi.server.*;
import java.lang.String;

interface RemoteInterface extends Remote
{
  public int add(int x,int y)throws Exception;
}

public class ServerImplements extends 
 
UnicastRemoteObject implements RemoteInterface
  {
  public ServerImplements()throws Exception
  {
  super();
  }
  public int add(int x,int y)
  {
  return (x+y);
  }
}

Server.java

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

public class  Server
{
  public static void main(String args[])
  {
  try
  {
  ServerImplements s=new ServerImplements();
  Naming.rebind("SERVICE",s);
  System.out.println("Server Started ");
  }
  catch(Exception e)
  {
  System.out.println(e.getMessage());
  }
  }

}

Client.java

import java.rmi.*;
import java.io.*;


public class Client
{
public static void main(String args[])
  {
 try
  {
 String ip="rmi://192.168.1.97/RMIAPPLICATION";
 RemoteInterface s=
   (RemoteInterface)Naming.lookup(ip);

 System.out.println("sum: "+ s.add(1,3));
  }
 catch(Exception e)
  {
  System.out.println(e.getMessage());
  e.printStackTrace();
  }
  }
}

Steps for executions

javac RemoteInterface.java

javac ServerImplements.java

rmic ServerImplements

javac Server.java

javac Client.java

start rmiregistry

start java server

java Client

Output:

Sum: 4

     

Advertisements

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: March 22, 2005

Ask Questions?    Discuss: RMI-Example-1   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
naga
July 20, 2012
rmi concept

i can complie this source code by follow above specify steps then i start rmiregistry bt i cant able to display the output sum 4
g.punitha
August 23, 2012
i cant get output in this program

sir/madam... all programs successfully compiled...bt finally after javac client not worked properly. i got some error after java client... Z:\rmi>java Client Connection refused to host: 192.168.1.97; nested exception is: java.net.ConnectException: Connection timed out: connect java.rmi.ConnectException: Connection refused to host: 192.168.1.97; nested exce ption is: java.net.ConnectException: Connection timed out: connect at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:601) at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198 ) at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184) at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322) at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source) at java.rmi.Naming.lookup(Naming.java:84) at Client.main(Client.java:12) Caused by: java.net.ConnectException: Connection timed out: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:519) at java.net.Socket.connect(Socket.java:469) at java.net.Socket.<init>(Socket.java:366) at java.net.Socket.<init>(Socket.java:180) at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirect SocketFactory.java:22) at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMaster SocketFactory.java:128) at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:595) ... 6 more
Lucas
October 20, 2012
Error

Hello, i have a problem when i try to compile javac ServerImplements.java, i get a message like this.. cannot find symbol symbol: class RemoteInterface. first i compile RemoteInterface, then is when i try to compile ServerImplements.java what is the problem ?
Shriram
January 4, 2013
rmiregistry

rmiregistry will not be work......please give me solution to my email id..
Chandra Shekhar Singh
December 8, 2013
Make correction in Server.java

Please make a correction in server.java as below- Naming.rebind("RMIAPPLICATION",s); compile the same and start server and client again. you will get result.
DMCA.com