Below are the Steps to write the RMI program
- Create the remote interface
- Implementation of the remote interface
- Compile the implementation class and create the stub and skeleton objects using the rmic tool
- Start the registry service
- Create and start the server application
- 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