0% found this document useful (0 votes)
62 views3 pages

6A. Basic Calculator

The document describes a basic remote calculator application implemented using Java RMI (Remote Method Invocation). It includes interfaces and classes for the remote object (CalcInf, CalcImpl), a server to register and bind the remote object (CalcServer), and a client to invoke methods on the remote object (CalcClient). The client takes two integer operands as arguments and calls all the basic arithmetic methods (add, sub, mul, div, rem) to calculate results which are printed on the console.

Uploaded by

Navin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views3 pages

6A. Basic Calculator

The document describes a basic remote calculator application implemented using Java RMI (Remote Method Invocation). It includes interfaces and classes for the remote object (CalcInf, CalcImpl), a server to register and bind the remote object (CalcServer), and a client to invoke methods on the remote object (CalcClient). The client takes two integer operands as arguments and calls all the basic arithmetic methods (add, sub, mul, div, rem) to calculate results which are printed on the console.

Uploaded by

Navin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PANIMALAR ENGINEERING COLLEGE

DEPARTMENT OF CSE
211416104185

BASIC CALCULATOR

CalcInf.java:

// Declares remote method interfaces--CalcInf.java import java.rmi.*;

public interface CalcInf extends Remote


{
public long add(int a, int b) throws RemoteException;
public int sub(int a, int b) throws RemoteException;
public long mul(int a, int b) throws RemoteException;
public int div(int a, int b) throws RemoteException;
public int rem(int a, int b) throws RemoteException;
}

CalcImpl.java:
// Implement Remote behavior--CalcImpl.java

import java.rmi.*;
import java.rmi.server.*;

public class CalcImpl extends UnicastRemoteObject implements


CalcInf
{
public CalcImpl() throws RemoteException { }

public long add(int a, int b) throws RemoteException


{
return a + b;
}
public int sub(int a, int b) throws RemoteException
{
int c = a > b ? a - b : b - a;
return c;
}
public long mul(int a, int b) throws RemoteException
{
return a * b;
}
public int div(int a, int b) throws RemoteException
{
return a / b;
}
public int rem(int a, int b) throws RemoteException
{
return a % b;
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
211416104185
}
}

CalcServer.java:
// Server that names the service implemented--CalcServer.java
import java.rmi.*;
public class CalcServer
{
public static void main(String args[])
{
try {
CalcInf C = new CalcImpl();
Naming.rebind("CalcService", C);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}

CalcClient.java:
// Client that invokes remote host methods--CalcClient.java
import java.rmi.*;
import java.net.*;
public class CalcClient
{
public static void main(String[] args) throws Exception
{
try {
CalcInf C = (CalcInf) Naming.lookup("rmi://" +
args[0] + "/CalcService");
int a, b;
if (args.length != 3)
{
System.out.println("Usage: java CalcClient
<remoteip> <operand1> <operand2>");
System.exit(-1);
}
a = Integer.parseInt(args[1]);
b = Integer.parseInt(args[2]);
System.out.println( "\nBasic Remote Calc\n" );
System.out.println("Summation : " + C.add(a, b));
System.out.println("Difference : " + C.sub(a, b));
System.out.println("Product : " + C.mul(a, b));
System.out.println("Quotient : " + C.div(a, b));
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
211416104185
System.out.println("Remainder : " + C.rem(a, b));
}
catch (Exception E) {
System.out.println(E.getMessage());
}
}
}

OUTPUT:
SERVER:

z:\>javac CalcInf.java

z:\>javac CalcImpl.java z:\>javac

CalcServer.java z:\>javac

CalcClient.java z:\>rmic CalcImpl

z:\>start rmiregistry

z:\>java CalcServer

CLIENT:

z:\>java CalcClient 172.16.6.45 6 8

Basic Remote Calc

Summation : 14
Difference :2
Product : 48
Quotient :0
Remainder :6

You might also like