Thursday, 6 December 2012

Factorial using RMI

RMI program for find factorial of a number

RMI interface

import java.rmi.*;
public interface myInterface extends Remote
{
    double fact(int i)throws RemoteException;
}

RMI server

import java.rmi.*;
import java.rmi.server.*;
public class myServer extends UnicastRemoteObject implements myInterface
{
    public myServer()throws RemoteException
    {
        super();
    }

    public double fact(int i)throws RemoteException
    {
        int f=1;
        int n=1;
        while(n<=i)
        {
            f=f*n;
            n++;
        }
        return(f);

    }
    public static void main(String args[])
    {
        try{
            myServer ms=new myServer();
            Naming.rebind("rmi://hsj./myInterface",ms);
/*  bolded area must replace with your computer name */
            System.out.print("Start...");
           
        }

        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
    }
}

RMI Client

import java.io.*;
import java.rmi.*;
public class myClient
{
    public static void main(String args[])throws IOException,RemoteException
    {
        int no;
           BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
           System.out.print("Enter a no:");
          no=Integer.parseInt(br.readLine());
        try{

        myInterface mi=(myInterface)Naming.lookup("rmi://hsj./myInterface");
        double f=mi.fact(no);
        System.out.print("Factorial of "+no+" is:"+f );

        }
        catch(Exception ex)
        {
        }
       
    }
}

output

rmi server program
RMI Server

RMI client
RMI Client


6 comments:

  1. very nice tutorial. its so simple to understand. i finally get it. and i managed to run it correctly. thank you! :))

    ReplyDelete

  2. This is quite educational arrange. It has famous breeding about what I rarity to vouch. Colossal proverb.
    This trumpet is a famous tone to nab to troths. Congratulations on a career well achieved. This arrange is synchronous s informative impolites festivity to pity. I appreciated what you ok extremely here 


    Selenium training in bangalore
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Thank You and that i have a keen offer you: How Much Remodel House split level house exterior remodel

    ReplyDelete