Implementation of Remote Procedure Call
A) Implement Server calculator containing
Add(), Mul(), Sub() etc.
B) Implementation of Date Time server containing date and time procedure.
(a) Implement Server calculator containing add(),mul(),sub().etc.
Calculator.java:
import java.rmi.*;
public interface Calculator extends Remote
{
public int sum(int a, int b)throws RemoteException;
public int sub(int a, int b)throws RemoteException;
public int mul(int a, int b)throws RemoteException;
}
CalculatorClient.java:
import java.rmi.*;
import java.io.*;
public class CalculatorClient
{
public static void main(String args[])
{
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Calculator p=(Calculator)Naming.lookup("Cal");
System.out.println("Enter fisrt number");
String strFirst=br.readLine();
System.out.println("Enter second number");
String strSecond=br.readLine();
System.out.println("Sum : "+p.sum(Integer.parseInt(strFirst),Integer.parseInt(strSecond)));
System.out.println("Sub : "+p.sub(Integer.parseInt(strFirst),Integer.parseInt(strSecond)));
System.out.println("Mul : "+p.mul(Integer.parseInt(strFirst),Integer.parseInt(strSecond)));
}
catch(Exception e)
{
System.out.println("Exception occurred : "+e.getMessage());
}
}
}
import java.io.*;
public class CalculatorClient
{
public static void main(String args[])
{
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Calculator p=(Calculator)Naming.lookup("Cal");
System.out.println("Enter fisrt number");
String strFirst=br.readLine();
System.out.println("Enter second number");
String strSecond=br.readLine();
System.out.println("Sum : "+p.sum(Integer.parseInt(strFirst),Integer.parseInt(strSecond)));
System.out.println("Sub : "+p.sub(Integer.parseInt(strFirst),Integer.parseInt(strSecond)));
System.out.println("Mul : "+p.mul(Integer.parseInt(strFirst),Integer.parseInt(strSecond)));
}
catch(Exception e)
{
System.out.println("Exception occurred : "+e.getMessage());
}
}
}
CalculatorServer.java:
import java.rmi.*;
import java.rmi.server.*;
public class CalculatorServer extends UnicastRemoteObject implements Calculator
{
public CalculatorServer()throws RemoteException
{
System.out.println("Server is Instantiated");
}
public int sum(int first,int Second) throws RemoteException
{
return first+Second;
}
public int sub(int first,int Second) throws RemoteException
{
return first-Second;
}
public int mul(int first,int Second) throws RemoteException
{
return first*Second;
}
public static void main(String arg[])
{
try{
CalculatorServer p=new CalculatorServer();
Naming.rebind("Cal",p);
}catch(Exception e)
{
System.out.println("Exception occurred : "+e.getMessage());
}
}
}
import java.rmi.server.*;
public class CalculatorServer extends UnicastRemoteObject implements Calculator
{
public CalculatorServer()throws RemoteException
{
System.out.println("Server is Instantiated");
}
public int sum(int first,int Second) throws RemoteException
{
return first+Second;
}
public int sub(int first,int Second) throws RemoteException
{
return first-Second;
}
public int mul(int first,int Second) throws RemoteException
{
return first*Second;
}
public static void main(String arg[])
{
try{
CalculatorServer p=new CalculatorServer();
Naming.rebind("Cal",p);
}catch(Exception e)
{
System.out.println("Exception occurred : "+e.getMessage());
}
}
}
Output:
b) Implement Date time server containing date() and time() procedure.
prac2i.java
import java.rmi.*;
public interface prac2i extends Remote
{
public String showdate()throws RemoteException;
public String showtime()throws RemoteException;
}
prac2s.java
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
public class prac2s extends UnicastRemoteObject implements prac2i
{
public prac2s()throws RemoteException
{
System.out.println("Server created");
}
public String showdate() throws RemoteException
{
Date d=new Date();
return d.toString();
}
public String showtime() throws RemoteException
{
Date d=new Date();
String s=d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
return s;
}
public static void main(String arg[])
{
try{
prac2s p=new prac2s();
Naming.rebind("xyz",p);
}catch(Exception e){}
}
}
import java.rmi.server.*;
import java.util.*;
public class prac2s extends UnicastRemoteObject implements prac2i
{
public prac2s()throws RemoteException
{
System.out.println("Server created");
}
public String showdate() throws RemoteException
{
Date d=new Date();
return d.toString();
}
public String showtime() throws RemoteException
{
Date d=new Date();
String s=d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
return s;
}
public static void main(String arg[])
{
try{
prac2s p=new prac2s();
Naming.rebind("xyz",p);
}catch(Exception e){}
}
}
prac2c.java
import java.rmi.*;
public class prac2c
{
public static void main(String args[])
{
try{
prac2i p=(prac2i)Naming.lookup("xyz");
System.out.println(p.showdate());
System.out.println(p.showtime());
}catch(Exception e){e.printStackTrace();}
}
}
public class prac2c
{
public static void main(String args[])
{
try{
prac2i p=(prac2i)Naming.lookup("xyz");
System.out.println(p.showdate());
System.out.println(p.showtime());
}catch(Exception e){e.printStackTrace();}
}
}
Output:
I need rpc program in java
ReplyDeleteYou have used java.rmi package here? So is this a RPC implementation or RMI implementation? What makes this different from a RMI communication?
ReplyDelete