Friday, March 4, 2016

implement RPC(Remote Procedure Call)

Aim:
To write a java program to implement RPC (remote procedure call)

Algorithm :

Step 1: start the program.
Step 2: include the necessary packages in java. 
Step 3: create an interface as ucet and extends the predefined interface remote into it.
Step 4: declare the remote methods inside the interface used.
Step 5: declare the class rpc and extends the predefined class.
Step 6: unicast remote object an implement the interface ucet into it.
Step 7: declare the class server &create an object ob for the class rpc.
Step 8: declare the class client
Step 9: call the remote methods using the object ob which is the object of the interface ucet.
Step 10: the remote method contains the methods such as functions(a,b),power(a,b)and log(a).
Step 11:Stop the program.

Program:

Client:

import java.rmi.*;
import java.io.*;
import java.rmi.server.*;
public class clientrpc
{
public static void main(String arg[])
{
try
{
String serverurl="rmi://localhost/serverrpc";
ucet ob=(ucet) Naming.lookup(serverurl);
int r=ob.function(10,5);
System.out.println("the answer of(a+b)^2 is:"+r);
int t =ob.power(10,5);
System.out.println("the answer of(a)^(b) is:"+t);
double d=ob.log(10);
System.out.println("the log value of the given number "+10+" is :"+d);
}
catch(Exception e)
{
System.out.println("error.."+e.getMessage());
}
}
}

Server:
import java.rmi.*;
import java.rmi.server.*;
public class serverrpc
{
public static void main(String arg[])
{
try
{
rpc ob=new rpc();
Naming.rebind("serverrpc",ob);
}
catch(Exception e)
{
}
}}
RPC:
import java.rmi.*;
import java.lang.Math.*;
import java.rmi.server.*;
public class rpc extends UnicastRemoteObject implements ucet
{
public rpc()throws Exception
{
}
public int function(int a,int b)throws RemoteException
{
int m;
m=(a*a)+(b*b)+(2*a*b);
return m;
}
public int power(int a,int b)throws RemoteException
{
int m=(int)Math.pow(a,b);
return m;
}
public double log(int a)throws RemoteException
{
return(Math.log(a));
}
}

Ucet:
import java.rmi.*;
public interface ucet extends Remote
{
public int function(int a,int b)throws RemoteException;
public int power(int a,int b)throws RemoteException;
public double log(int a)throws RemoteException;
}


Output:
Javac ucet.java
Start rmiregistry

Javac rpc.java
rmi rpc
javac clientrpc.java
javac serverrpc.java
javac rpc.java
javac ucet.java

javac serverrpc.java
java serverrpc

javac clientrpc.java
java clientrpc
the ans of (a+b)^2 is:225
the ans of (a)^(b) is :100000
the log value of the given number 10 is 2.30258



No comments:

Post a Comment