Sie sind auf Seite 1von 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