Java Blog

 
 
 
// CountRMIImpl.java, CountRMI implementation

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

public class CountRMIImpl extends UnicastRemoteObject
       implements CountRMI
{
  private int sum;

  public CountRMIImpl(String name) throws RemoteException
  {
    super();
    try
    {
      Naming.rebind(name, this);
      sum = 0;
    } catch (Exception e)
    { System.out.println(\"Exception: \" + e.getMessage());
      e.printStackTrace();
    }
  }

  public int sum() throws RemoteException
  { return sum;
  }

  public  void sum(int val) throws RemoteException
  { sum = val;
  }

  public int increment() throws RemoteException
  { sum++;
    return sum;
  }
}


// CountRMIClient.java  RMI Count client

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

public class CountRMIClient
{ public static void main(String args[])
  { // Create and install the security manager
    System.setSecurityManager(new RMISecurityManager());

    try
    { 
        //CountRMI myCount = (CountRMI)Naming.lookup(\"rmi://\"
        //                  + args[0] + \"/\" + \"myCountRMI\");
    CountRMI myCount = (CountRMI)Naming.lookup(\"//\"
                          + args[0] + \"/\" + \"myCountRMI\");
      // Set Sum to initial value of 0
      System.out.println(\"Setting Sum to 0\");
      myCount.sum(0);

      // Calculate Start time
      long startTime = System.currentTimeMillis();

      // Increment 1000 times
      System.out.println(\"Incrementing\");
      for (int i = 0 ; i < 1000 ; i++ )
      { myCount.increment();
      }

      // Calculate stop time; print out statistics
      long stopTime = System.currentTimeMillis();
      System.out.println(\"Avg Ping = \"
                       + ((stopTime - startTime)/1000f)
                       + \" msecs\");
      System.out.println(\"Sum = \" + myCount.sum());
    } catch(Exception e)
    { System.err.println(\"System Exception\" + e);
    }
   System.exit(0);
  }
}



// CountRMIServer.java

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

public class CountRMIServer
{

 public static void main(String args[])
 { 

   // Create and install the security manager
   System.setSecurityManager(new RMISecurityManager());

   try
   {
     // Create CountRMIImpl
     CountRMIImpl myCount = new CountRMIImpl(\"//xyz/myCountRMI\");
     System.out.println(\"CountRMI Server ready.\");
   } catch (Exception e)
   { System.out.println(\"Exception: \" + e.getMessage());
     e.printStackTrace();
   }
 }
}

    Related Post:
  1. Program to show the use of Bitwise Operator (|) OR

  2. An applet program to draw concentric circle in center of the canvas

  3. Program to show the use of Labeled Break Statement

  4. Program to declare, initialize and print an array of characters

  5. Program to display triangle of numbers

  6. Program which creates an Array of character. Make one function with one argument as a character and that function throw a user defined exception

  7. Program to show an example of using initialization block to initialize the data members of a class

  8. RMI application for counting factorial of a given number.

  9. Program to generate SQUARE ROOT table

  10. Program to show an example of Calling a Base Class Method from a Derived Class

  11. Program to compute the sum of the digits of a given integer number. eg: I/o=4 then O/p=4+3+2+1=10

  12. CORBA based application for addition of two mathematical expressions of the form (ax + by) and displaying the result on client side

  13. Program to show an example of Multiple Levels of Inheritance

  14. program which takes a long sentence from the client passes to the server,server separates out each word and sends them back to client in reverse order

  15. An applet program that displays Text at the center of the string which is passed as parameter

  16. Application for counting factorial of a given number

  17. Program in CORBA which takes a long sentence from the client (at least 7 words), passes to the server, server separates out each word

  18. Program to search an element in an array using Linear Search

  19. Program of writing Bytes to a File

  20. Develop a simple calculator with addition, subtraction, multiplication and division capabilities, data collection from client side, executed on server

 
 
Didn't find what you were looking for? Find more on RMI program of count server and client