Searching...
Saturday 24 May 2014

RMI PROGRAM TO SIMPLE CALCULATOR

5/24/2014 08:44:00 am

Interface

 

import java.rmi.Remote;

public interface calculator extends Remote {

    public long add(long a, long b) throws java.rmi.RemoteException;

    public long sub(long a, long b) throws java.rmi.RemoteException;

    public long mul(long a, long b) throws java.rmi.RemoteException;

    public long div(long a, long b) throws java.rmi.RemoteException;

}

 

Server:-

 

import java.rmi.RemoteException;

import java.rmi.registry.LocateRegistry;

import java.rmi.server.UnicastRemoteObject;

import java.rmi.registry.Registry;

 

public class Rmicalc implements calculator{   

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

        return a+b;    }

    public long sub(long a, long b) throws RemoteException {

        return a-b;    }

    public long mul(long a, long b) throws RemoteException {

        return a*b;    }

    public long div(long a, long b) throws RemoteException {

        return a/b;    }

    public static void main(String[] args) throws RemoteException {

        Rmicalc rm=new Rmicalc();

        calculator stub=(calculator) UnicastRemoteObject.exportObject(rm,0);

        Registry reg=LocateRegistry.getRegistry();

        reg.rebind("kkp", stub);

            System.out.println("server is ready");

    }   

}

 

Client:-

 

import java.lang.invoke.MethodHandles;

import java.rmi.NotBoundException;

import java.rmi.Remote;

import java.rmi.RemoteException;

import java.rmi.registry.LocateRegistry;

import java.rmi.registry.Registry;

 

public class client  {

    public static void main(String s[]) throws RemoteException, NotBoundException

    {

        Registry reg=LocateRegistry.getRegistry("localhost");

        calculator skelton=(calculator)reg.lookup("kkp");

        System.out.println();

        System.out.println("Addition of 4 and 5 is :" + skelton.add(4, 5));

        System.out.println("Substraction of 9 and 5 is :" + skelton.sub(9, 5));

        System.out.println("Multiplication of 8 and 3 is :" + skelton.mul(8, 3));

        System.out.println("Division of 16 and 4 is :" + skelton.div(16, 4));

    }     


}

0 comments:

Post a Comment