Excercises Using Rmi
Excercises Using Rmi
and division of RMI. Procedure: Step 1: Create AllServerIntf.java which defines the remote interface that is provided by the server. Step 2: This file contains four methods that accept two double arguments and returns the calculated value. Step 3: The second file to be created is AllServerImpl.java which implements the remote interface. Step 4: This java file extends unicast remote object which provides functionality that is needed to make object available from remote machines. Step 5: The next file created is AllServer.java which will update the rmi registry on that machine. This is done by rebind () method of naming class. Step 6: The last file is AllClient.java which implements the client side of this distributed application. Step 7: This file requires four command line arguments which are IP address, two arguments from the computation and the arithmetic operator name. Step 8: Compile and execute the program according to the settings given below.
The compilation of the source code can be done by command line,as shown here:
javac AllServerIntf.java javac AllServerImpl.java javac AllServer.java javac AllClient.java Step Two: Generate Stubs and Skeletons To generate Stubs and Skeletons,we use a tool called the RMI compiler,which is invoked from the command line, rmic AllServerImpl Step Three: Install Files on the Client and Server Machines Copy AllClient.class,AllServerImpl_Stub.class and AllServerIntf.class to a directory on the client machine. Copy AllServerIntf.class,AllServerImpl.class,AllServerImpl_Skel.class, AllServerImpl_Stub and AllServer.class to a directory on the server machine. Step Four: Start the RMI Registry on the Server Machine The JDK provides a program called rmiregistry,which executes on the server machine.It maps names to object references.Start the RMI Registry from the command line as show below: start rmiregistry Step five: Start the Server
The server code is started from the command line: java AllServer Step Six: Start the Client
The client code can be invoked by the command line by using one of the two formats shown below:
java AllClient localhost 5 4 add java AllClient 172.16.11.162 5 4 add PROGRAM CODING: AllserverIntf.java: import java.rmi.*; public interface AllServerIntf extends Remote { double add(double d1,double d2)throws RemoteException; double sub(double d1,double d2)throws RemoteException; double mul(double d1,double d2)throws RemoteException; double div(double d1,double d2)throws RemoteException; } AllServerImpl.java: import java.rmi.*; import java.rmi.server.*; public class AllServerImpl extends UnicastRemoteObject implements AllServerIntf { public AllServerImpl() throws RemoteException { } public double add(double d1,double d2) throws RemoteException { return d1+d2; } public double sub(double d1,double d2) throws RemoteException
{ return d1-d2; } public double mul(double d1,double d2) throws RemoteException { return d1*d2; } public double div(double d1,double d2) throws RemoteException { return d1/d2; } } AllClient.java: import java.rmi.*; public class AllClient{ public static void main(String args[]){ try{ String allserverURL="rmi://"+args[0]+"/AllServer"; AllServerIntf allserverintf=(AllServerIntf)Naming.lookup(allserverURL); System.out.println("the first number is"+args[1]); double d1=Double.valueOf(args[1]).doubleValue(); System.out.println("the second number is:"+args[2]); double d2=Double.valueOf(args[2]).doubleValue(); if(args[3].equals("add")) {
System.out.println("the sum is:"+allserverintf.add(d1,d2)); } else if(args[3].equals("sub")) { System.out.println("the sum is:"+allserverintf.sub(d1,d2)); } else if(args[3].equals("mul")) { System.out.println("the sum is:"+allserverintf.mul(d1,d2)); } else if(args[3].equals("div")) { System.out.println("the sum is:"+allserverintf.div(d1,d2)); } } catch(Exception e) { System.out.println("Exception:"+e); } } } AllServer.java: import java.rmi.*; import java.net.*; public class AllServer
SERVER SIDE:
SUBRACTION:
MULTIPLICATION:
DIVISION:
RESULT:
Thus the program for performing arithmetic operations like addition, subtraction, multiplication and division using RMI has been executed and verified.
SIMPLE AND COMPOUND INTEREST USING RMI Aim: To develop a java program for calculating simple interest and compound interest using RMI. Procedure: Step 1: Create SimpleServerIntf.java which defines the remote interface that is provided by the server. Step 2: This java file contains methods for calculating simple interest and compound interest Step 3: Then the implementation is done by the file SimpleServerImpl.java which implements the remote interface. Step 4: This java file extends unicast remote object which provides functionality that is needed to make object available from remote machines. Step 5: The SimpleServer.java which will update the rmi registry on that machine.This is done by rebind( ) method of naming class. Step 6: The last java file needed is SimpleClient.java which implements the client side of this distributed application. Step 7: This file requires four command line arguments which are IP address, two arguments from the computation and the arithmetic operator name. Step 8: Compile and execute the program according to the settings given. Settings: Step one: Enter and compile the source code
The compilation of the source code can be done by command line,as shown here: javac simpleServerIntf.java javac simpleServerImpl.java javac simpleServer.java
javac simpleClient.java Step Two: Generate Stubs and Skeletons To generate Stubs and Skeletons,we use a tool called the RMI compiler,which is invoked from the command line, rmic simpleServerImpl Step Three: Install Files on the Client and Server Machines Copy simpleClient.class,simpleServerImpl_Stub.class and simpleServerIntf.class to a directory on the client machine. Copy simpleServerIntf.class,simpleServerImpl.class,simpleServerImpl_Skel.class, simpleServerImpl_Stub and simpleServer.class to a directory on the server machine. Step Four: Start the RMI Registry on the Server Machine The JDK provides a program called rmiregistry,which executes on the server machine.It maps names to object references.Start the RMI Registry from the command line as show below: start rmiregistry Step Five: Start the Server
The server code is started from the command line: java simpleServer Step Six: Start the Client
The client code can be invoked by the command line by using one of the two formats shown below: java simpleClient localhost 2 100 200 si java simpleClient 172.16.11.162 2 100 200 si
import java.rmi.*; public interface simpleServerIntf extends Remote { double calculate(double r,double p,double n)throws RemoteException; double comcalculate(double r,double p,double n)throws RemoteException; } simpleServerImpl.java import java.rmi.*; import java.rmi.server.*; import java.lang.*; public class simpleServerImpl extends UnicastRemoteObject implements simpleServerIntf { public simpleServerImpl()throws RemoteException { } public double calculate(double r,double p,double n)throws RemoteException { return((n*p*r)/100); } public double comcalculate(double r,double p,double n)throws RemoteException { double res=1+r/100; return(p*Math.pow(res,n)); }
} simpleServer.java import java.rmi.*; import java.net.*; public class simpleServer { public static void main(String args[]) { try { simpleServerImpl siserverimpl=new simpleServerImpl(); Naming.rebind("simpleServer",siserverimpl);
} catch(Exception e) { System.out.println("Exception"+e); } } } simpleClient.java import java.rmi.*; public class simpleClient { public static void main(String args[])
{ try { String siserverURL="rmi://"+args[0]+"/simpleServer"; simpleServerIntf simpleserverintf=(simpleServerIntf)Naming.lookup(siserverURL); System.out.println("The No. of year is:"+args[1]); double r=Double.valueOf(args[1]).doubleValue(); System.out.println("The Principal Amt is:"+args[2]); double p=Double.valueOf(args[2]).doubleValue(); System.out.println("The Rate Percentage is:"+args[3]); double n=Double.valueOf(args[3]).doubleValue(); if(args[4].equals("si")) { System.out.println("The Simplest Interest is:"+simpleserverintf.calculate(r,p,n)); } if(args[4].equals("ci")) { System.out.println("The Compound Interest is"+simpleserverintf.comcalculate(r,p,n)); } } catch(Exception e) { System.out.println("Exception:"+e); } }
OUTPUT: SERVER :
RMI REGISTRY:
CLIENT:
REMOTE MACHINE
RESULT:
Thus the program for calculating simple interest and compound interest using RMI has been executed and verified. ATM USING RMI Aim: To develop a java program for ATM service using RMI.
Procedure: Step 1: Create atmServerIntf.java which defines the remote interface that is provided by the server. Step 2: This java file contains four methods that accepts one string arguments and returns the calculated value. Step 3: Then second java file to be created is atmServerImpl.java which implements the remote interface. Step 4: This java file extends unicast remote object which provides functionality that is needed to make object available from remote machines. Step 5: The next file to be created is atmServer.java which will update the RMI registry on that machine.This is done by rebind( ) method of naming class. Step 6: The last java file needed is atmClient.java which implements the client side of this distributed application. Step 7: This file requires four command line arguments which are IP address, two arguments from the computation and the arithmetic operator name. Step 8: Compile and execute the program according to the settings given. Settings: Step one: Enter and compile the source code
The compilation of the source code can be done by command line,as shown here:
Step Two: Generate Stubs and Skeletons To generate Stubs and Skeletons,we use a tool called the RMI compiler,which is invoked from the command line, rmic atmServerImpl Step Three: Install Files on the Client and Server Machines Copy atmClient.class,atmServerImpl_Stub.class and atmServerIntf.class to a directory on the client machine. Copy atmServerIntf.class, atmServerImpl.class, atmServerImpl_Skel.class, atmServerImpl_Stub and atmServer.class to a directory on the server machine. Step Four: Start the RMI Registry on the Server Machine The JDK provides a program called rmiregistry,which executes on the server machine.It maps names to object references.Start the RMI Registry from the command line as show below: start rmiregistry Step five: Start the Server
The server code is started from the command line: java atmServer Step Six: Start the Client
The client code can be invoked by the command line by using one of the two formats shown below: java atmClient
PROGRAM CODE atmServerIntf.java import java.rmi.*; public interface atmServerIntf extends Remote {
double deposit(double dep,double bal) throws RemoteException; double withdraw(double wit,double bal) throws RemoteException; } atmServerImpl.java import java.rmi.*; import java.rmi.server.*; public class atmServerImpl extends UnicastRemoteObject implements atmServerIntf { public atmServerImpl()throws RemoteException { } public double deposit(double dep,double bal)throws RemoteException { return(dep+bal); } public double withdraw(double wit,double bal)throws RemoteException { return(bal-wit); } } atmClient.java import java.rmi.*; import java.io.*; public class atmClient {
public static void main(String args[]) { try { String siserverURL="rmi://localhost/atmServer"; atmServerIntf atmserverintf=(atmServerIntf)Naming.lookup(siserverURL); double dep,wit,bal; dep=0;wit=0;bal=0; int n=0; while(n==0) { System.out.println("\n ATM BANK\n"); System.out.println("1.Deposit"); System.out.println("2.withdrawl"); System.out.println("3.Balance"); System.out.println("4.Exit\n"); System.out.println("Enter ur choice:\t"); BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); int ch=Integer.parseInt(bf.readLine()); switch(ch) { case 1:System.out.println("Amt to be deposited: \t"); dep=Double.parseDouble(bf.readLine()); bal=atmserverintf.deposit(dep,bal); System.out.println("Your current deposit is:"+dep);
break; case 2: System.out.println("Amount to be withdrawn: \t"); wit=Double.parseDouble(bf.readLine()); bal=atmserverintf.withdraw(wit,bal); System.out.println("Withdrawl is: "+wit); break; case 3: System.out.println("Balance is :"+bal); break; case 4: System.out.println("thank u :"); n=1; break; } } } catch(Exception e) { System.out.println("Exception"+e); } } } atmServer.java import java.rmi.*;
import java.net.*; public class atmServer { public static void main(String args[]) { try { atmServerImpl siserverimpl=new atmServerImpl(); Naming.rebind("atmServer",siserverimpl); } catch(Exception e) { System.out.println("Exception: "+e); } } } OUTPUT: SERVER :
RMI REGISTRY:
CLIENT:
RESULT: Thus the program for ATM service using RMI has been executed and verified. TELEPHONE DIRECTORY USING RMI Aim:
To develop a java program for Telephone directory using RMI. Procedure: Step 1: Create TelephoneIntf.java which defines the remote interface that is provided by the server. Step 2: This java file contains four methods that accepts one string arguments and returns the calculated value. Step 3: Then second java file to be created is TelephoneImpl.java which implements the remote interface. Step 4: This java file extends unicast remote object which provides functionality that is needed to make object available from remote machines. Step 5: The next file to be created is TelephoneServer.java which will update the RMI registry on that machine.This is done by rebind( ) method of naming class. Step 6: The last java file needed is TelephoneClient.java which implements the client side of this distributed application. Step 7: This file requires four command line arguments which are IP address, two arguments from the computation and the arithmetic operator name. Step 8: Compile and execute the program according to the settings given. Settings: Step one: Enter and compile the source code
The compilation of the source code can be done by command line,as shown here: javac TelephoneServerIntf.java javac TelephoneServerImpl.java javac TelephoneServer.java javac TelephoneClient.java Step Two: Generate Stubs and Skeletons
To generate Stubs and Skeletons,we use a tool called the RMI compiler,which is invoked from the command line, rmic TelephoneServerImpl Step Three: Install Files on the Client and Server Machines Copy TelephoneClient.class, TelephoneServerImpl_Stub.class and TelephoneServerIntf.class to a directory on the client machine. Copy TelephoneServerIntf.class, TelephoneServerImpl.class, TelephoneServerImpl_Skel.class, TelephoneServerImpl_Stub and TelephoneServer.class to a directory on the server machine. Step Four: Start the RMI Registry on the Server Machine The JDK provides a program called rmiregistry,which executes on the server machine.It maps names to object references.Start the RMI Registry from the command line as show below: start rmiregistry Step five: Start the Server
The server code is started from the command line: java TelephoneServer Step Six: Start the Client
The client code can be invoked by the command line by using one of the two formats shown below: java TelephoneClient
PROGRAM CODING: TelephoneIntf.java: import java.rmi.*; public interface TelephoneIntf extends Remote
{ public String readnumber(double ph) throws RemoteException; public double readname(String na) throws RemoteException; } TelephoneImpl.java: import java.rmi.*; import java.rmi.server.*; import java.sql.*; import java.io.*; public class TelephoneImpl extends UnicastRemoteObject implements TelephoneIntf { public TelephoneImpl() throws RemoteException { } public String readnumber(double ph) throws RemoteException { String d=""; PreparedStatement ps; Connection con; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:telephone"); ps=con.prepareStatement("select * from tele where number=?"); ps.setDouble(1,ph);
} } catch(Exception ex) { ex.printStackTrace(); System.out.println("Hello it is error Marshall"); return d; } } public double readname(String na) throws RemoteException { double d; PreparedStatement ps; Connection con;
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:telephone"); ps=con.prepareStatement("select * from tele where name1=?"); ps.setString(1,na); ResultSet rs= ps.executeQuery(); if(rs.next()) { return Double.valueOf(rs.getString(1)).doubleValue(); } else { return 0; } } catch(Exception ex) { ex.printStackTrace(); return 0; } } } TelephoneServer.java: import java.net.*;
import java.rmi.*; public class TelephoneServer { public static void main(String args[]) { try { TelephoneImpl TelephoneServerImpl=new TelephoneImpl (); Naming.rebind ("TelephoneServer", TelephoneServerImpl); } catch(Exception e) { System.out.println("Exception:"+e); } } TelephoneClient.java: import java.rmi.*; public class TelephoneClient { public static void main(String args[]) { try { String allserverURL = "rmi://" + args[0] + "/TelephoneServer"; TelephoneIntf telephoneserverintf =(TelephoneIntf)Naming.lookup(allserverURL);
/* System.out.println("The first number is " +args[1]); double d1 = Double.valueOf(args[1]).doubleValue(); System.out.println("The second number is : " +args[2]); double d2 = Double.valueOf(args[2]).doubleValue();*/ if(args[1].equals("number")) { double d1 = Double.valueOf(args[2]).doubleValue(); //System.out.println("the give number is" +d1); System.out.println("The name is : " +telephoneserverintf.readnumber(d1)); } else { String d1 = args[2]; System.out.println("The number is : " +telephoneserverintf.readname(d1)); }} catch(Exception e) { System.out.println("Exception : "+e); }} } OUTPUT: SERVER :
RMI REGISTRY:
CLIENT:
REMOTE MACHINE:
DATABASE:
RESULT:
Thus the program for Telephone directory using RMI has been executed and verified. AIRLINE RESERVATION USING RMI Aim:
To develop a java program for Airline Reservation using RMI. Procedure: Step 1: Create AirIntf.java which defines the remote interface that is provided by the server. Step 2: This java file contains four methods that accepts one string arguments and returns the calculated value. Step 3: Then second java file to be created is AirImpl.java which implements the remote interface. Step 4: This java file extends unicast remote object which provides functionality that is needed to make object available from remote machines Step 5: The next file to be created is AirServer.java which will update the RMI registry on that machine.This is done by rebind( ) method of naming class. Step 6: The last java file needed is AirClient.java which implements the client side of this distributed application. Step 7: This file requires four command line arguments which are IP address, two arguments from the computation and the arithmetic operator name. Step 8: Compile and execute the program according to the settings given. Settings: Step one: Enter and compile the source code
The compilation of the source code can be done by command line,as shown here: javac AirServerIntf.java javac AirServerImpl.java javac AirServer.java javac AirClient.java Step Two: Generate Stubs and Skeletons
To generate Stubs and Skeletons,we use a tool called the RMI compiler,which is invoked from the command line, rmic AirServerImpl Step Three: Install Files on the Client and Server Machines Copy AirClient.class, AirServerImpl_Stub.class and AirServerIntf.class to a directory on the client machine. Copy AirServerIntf.class, AirServerImpl.class, AirServerImpl_Skel.class, AirServerImpl_Stub and AirServer.class to a directory on the server machine. Step Four: Create the ODBC DSN The steps for creating ODBC Data Source Name, 1. Go to start>setting>control panel>Administrative Tools 2. Double Click Data Source (ODBC) and then click Add button. 3. Now select Microsoft access driver and press the Finish button. 4. Type the Data Source Name in the text box. 5. Click the select button and choose the data base which you have Now click the ok button in the above dialog box. Step five: Start the RMI Registry on the Server Machine created.
The JDK provides a program called rmiregistry,which executes on the server machine.It maps names to object references.Start the RMI Registry from the command line as show below: start rmiregistry Step six: Start the Server
The server code is started from the command line: java AirServer Step Seven: Start the Client The client code can be invoked by the command line by using one of the two formats shown below: java AirClient
PROGRAM CODE: AirIntf.java import java.rmi.*; public interface AirIntf extends Remote { public String readregno(double reg) throws RemoteException; public double readname(String na) throws RemoteException; public String readceatno(double cn) throws RemoteException; //public double readfname(String fn) throws RemoteException; //public double readdate(String da) throws RemoteException; } AirImpl.java import java.rmi.server.*; import java.sql.*; import java.io.*; public class AirImpl extends UnicastRemoteObject implements AirIntf { public AirImpl() throws RemoteException { } public String readregno(double reg) throws RemoteException { String d=""; PreparedStatement ps; Connection con;
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:air"); ps=con.prepareStatement("select * from air where regno=?"); ps.setDouble(1,reg); ResultSet rs= ps.executeQuery(); if(rs.next()) { d=rs.getString(2); return d; }
else { d=null; return d; }} catch(Exception ex) { ex.printStackTrace(); System.out.println("Hello it is error Marshall"); return d; } } public double readname(String na) throws RemoteException
{ double d; PreparedStatement ps; Connection con; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:air"); ps=con.prepareStatement("select * from air where name=?"); ps.setString(1,na); ResultSet rs= ps.executeQuery(); if(rs.next()) { return Double.valueOf(rs.getString(1)).doubleValue(); } else { return 0; }} catch(Exception ex) { ex.printStackTrace(); return 0; }} AirServer.java
import java.net.*; import java.rmi.*; public class AirServer { public static void main(String args[]) { try { AirImpl AirServerImpl=new AirImpl (); Naming.rebind ("AirServer", AirServerImpl); } catch(Exception e) { System.out.println("Exception:"+e); }}} AirClient.java import java.rmi.*; public class AirClient { public static void main(String args[]) { try { String allserverURL = "rmi://" + args[0] + "/AirServer"; AirIntf airserverintf =(AirIntf)Naming.lookup(allserverURL);
/* System.out.println("The first number is " +args[1]); double d1 = Double.valueOf(args[1]).doubleValue(); System.out.println("The second number is : " +args[2]); double d2 = Double.valueOf(args[2]).doubleValue();*/ if(args[1].equals("regno")) { double d1 = Double.valueOf(args[2]).doubleValue(); System.out.println("The name is : " +airserverintf.readregno(d1)); double c1 = Double.valueOf(args[2]).doubleValue(); System.out.println("The ceat No is : " +airserverintf.readceatno(c1));
} else if(args[1].equals("ceatno")) { double d1 = Double.valueOf(args[2]).doubleValue(); System.out.println("The name is : " +airserverintf.readregno(d1)); } else { String d1 = args[2]; System.out.println("The Register No is : " +airserverintf.readname(d1)); } } catch(Exception e) {
RMI REGISTRY:
CLIENT:
DATABASE:
RESULT: Thus the program for Airline reservation using RMI has been executed and verified.