Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » Remote Method InvocationRSS Feeds

Develop a RMI based application for addition of two complex numbers

Posted By: Lilly Campbell     Category: Java     Views: 8288

Develop a RMI based application for addition of two complex numbers.

Code for Develop a RMI based application for addition of two complex numbers in Java

----------------------------------------------------------------------------------
                    Interface
----------------------------------------------------------------------------------


publicinterface sumInterface extends java.rmi.Remote
{
    int find_sum(int n1,int n2) throws java.rmi.RemoteException;
}

-------------------------------------------------------------------------------------------------
                    Implementation
-------------------------------------------------------------------------------------------------

import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.UnicastRemoteObject;

publicclass sumImpl extends UnicastRemoteObject
               implements sumInterface
{
    public sumImpl(String name) throws RemoteException
    {
        super();
        try
        {
            Naming.rebind(name,this);
        }
        catch(Exception e) { }
    }

    publicint find_sum(int n1,int n2) throws RemoteException
    {
        int ans=0;
        ans = n1+n2;
        return ans;
    }

}

-------------------------------------------------------------------------------------------------
                    Client
-------------------------------------------------------------------------------------------------

import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.io.DataInputStream;

publicclass sumClient
{
    publicstaticvoid main(String args[])
    {
        System.setSecurityManager(new RMISecurityManager());

        try
        {
            int ans;
            int intNumber1 = 0;
            int intNumber2 = 0;
            sumInterface pi = (sumInterface) Naming.lookup("//localhost/pno");
            DataInputStream in = new DataInputStream(System.in);;

            System.out.print("Enter Number 1 : ");
            intNumber1 = Integer.parseInt(in.readLine());

            System.out.print("Enter Number 2 : ");
            intNumber2 = Integer.parseInt(in.readLine());

            ans = pi.find_sum(intNumber1,intNumber2);
            System.out.println(ans);
        }
        catch(Exception e) { }
    }
}    

-------------------------------------------------------------------------------------------------
                    Server
-------------------------------------------------------------------------------------------------

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

publicclass sumServer
{
    publicstaticvoid main(String args[])
    {
        System.setSecurityManager(new RMISecurityManager());

        try
        {
            sumImpl pim = new sumImpl("//localhost/pno");
            System.out.println("\nServer is ready...");
        }
        catch(Exception e) { }
    }
}    

-------------------------------------------------------------------------------------------------
                    Policy
-------------------------------------------------------------------------------------------------

grant
{
    // Allow everything for now
    permission java.security.AllPermission;
};

-------------------------------------------------------------------------------------------------
                    Output
-------------------------------------------------------------------------------------------------

Enter Number 1 : 5
Enter Number 2 : 8
13
  
Share: 



Lilly Campbell
Lilly Campbell author of Develop a RMI based application for addition of two complex numbers is from Toronto, Canada.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!