Record 3:Calculator:
Create a remote server that implements a calculator with basic functionalities like addition, subtraction, division, multiplication and client, which uses the remote calculator.
-----------------------------------------------------------------------------------------
Program 1: Interface program calc.java
import java.rmi.*;
public interface calc extends Remote
{
float add(float a,float b) throws RemoteException;
float sub(float a,float b) throws RemoteException;
float mul(float a,float b) throws RemoteException;
float div(float a,float b) throws RemoteException;
double power(float base,float power) throws RemoteException;
}
Program 2: Implementation program calcImp.java
import java.rmi.*;
import java.rmi.server.*;
import java.lang.Math.*;
public class calcImp extends UnicastRemoteObject implements calc
{
public calcImp() throws RemoteException
{
super();
}
public float add(float a,float b) throws RemoteException
{
float result=a+b;
return result;
}
public float sub(float a,float b) throws RemoteException
{
float result=(float)a-b;
return result;
}
public float mul(float a,float b) throws RemoteException
{
float result=(float)a*b;
return result;
}
public float div(float a,float b) throws RemoteException
{
//if(b==0) return 0;
float result=(float)a/b;
return result;
}
public double power(float base,float power) throws RemoteException
{
double result= (double)Math.pow(base,power);
return result;
}
}
Program 3: Server program calcServer.java
import java.rmi.*;
import java.rmi.server.*;
public class calcServer
{
public static void main(String[] args)
{
try
{
System.out.print("Constructing Server Implementation...");
calcImp calc1 = new calcImp();
System.out.print("Done!nBinding Server Implementation...");
Naming.rebind("RMICalc",calc1);
System.out.print("Done!nWaiting for Invocations from client...n");
}
catch(Exception e)
{ System.out.print("nError Generated in calcServer : "+e.toString()+"n");
}
}
}
Program 4: Client program calcTClient.java
import java.rmi.*;
import java.rmi.server.*;
import java.io.*;
public class calcClient
{
public static void main(String[] args)
{
float o1,o2;
int choice=0;
System.setSecurityManager(new RMISecurityManager());
try
{
System.out.print("Naming.lookup for Bindings...");
calc calcu = (calc) Naming.lookup("RMICalc");
System.out.print("Done!n");
DataInputStream dis = new DataInputStream(System.in);
while(choice!=6)
{
System.out.println("n1. Addn2. Subtractn3. Multiplyn4. Dividen5. Powern6. Exit");
System.out.print("Enter your choice : ");
choice = Integer.parseInt(dis.readLine());
if(choice==6) break;
System.out.print("Enter first Number : ");
o1 = Float.parseFloat(dis.readLine());
System.out.print("Enter second Number : ");
o2 = Float.parseFloat(dis.readLine());
switch(choice)
{
case 1: System.out.println(o1+"+"+o2+"="+calcu.add(o1,o2)); break;
case 2: System.out.println(o1+"-"+o2+"="+calcu.sub(o1,o2)); break;
case 3: System.out.println(o1+"*"+o2+"="+calcu.mul(o1,o2)); break;
case 4: System.out.println(o1+"/"+o2+"="+calcu.div(o1,o2)); break;
case 5: System.out.println(o1+"^"+o2+"="+calcu.power(o1,o2)); break;
default: System.out.println("Invalid Choice");
}
}
}
catch(Exception e)
{ System.out.print("Error Generated in commServer : "+e.toString()+"n");
}
System.exit(0);
}
}
------------------------------------------------------------
Program 5: Show Bindings showBingings.java
import java.rmi.*;
import java.rmi.server.*;
public class showBindings
{
public static void main(String[] args)
{
try
{
String[] bindings = Naming.list("");
for(int i=0;i<bindings.length;i++)
System.out.println("Binding ["+(i+1)+"] => "+bindings[i]);
}
catch(Exception e)
{ System.out.println("Error in showBindings : "+e.toString()); }
}
}
Program 6: clinet.policy clinet.policy
grant
{
permission java.net.SocketPermission
"*:1024-65335","connect";
};
1. compile all java files
javac *.java
2. use RMICompiler to create stubs & skeletons of Implementations
rmic calcImp
3. run rmiregistry
start rmiregistry
4. create policy file with following contents
client.policy
~~~~~~~~~~~~~
grant
{
permission java.net.SocketPermission
"*:1024-65335","connect";
};
5. run server
java calcServer
6. run client with policy created in step 4.
java -Djava.security.policy=client.policy calcClient