Wednesday, 30 March 2016

Steps to write the RMI program with example program

Below are the Steps to write the RMI program

  1. Create the remote interface
  2. Implementation of the remote interface
  3. Compile the implementation class and create the stub and skeleton objects using the rmic tool
  4. Start the registry service 
  5. Create and start the server application
  6. Create and start the client application
Below are the example program with output:-

//Create the remote interface


import java.rmi.*;
public interface AdderApps extends Remote{

public int addition(int x,int y)throws RemoteException;
}


//Implementation of the remote interface


import java.rmi.*;
import java.rmi.server.*;

public class AdderRemoteobj extends UnicastRemoteObject implements AdderApps{

AdderRemoteobj()throws RemoteException{
super();
}

public int addition(int x,int y){return x+y;}

}

//Compile the implementation class and create the stub and skeleton objects using the rmic tool and start the registry service

//Create and start the server application

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

public class MyServer{

public static void main(String args[]){
try{

AdderApps stub=new AdderRemoteobj();
Naming.rebind("rmi://localhost:5000/Sai",stub);

}catch(Exception e){System.out.println(e);}
}

}

//Create and start the client application

import java.rmi.*;

public class MyClient{

public static void main(String args[]){
try{

AdderApps stub=(AdderApps)Naming.lookup("rmi://localhost:5000/Sai");
System.out.println(stub.addition(25,50));

}catch(Exception e){System.out.println(e);}
}

}


Output:-


No comments:

Post a Comment