Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: RMI Problems with doing calculations across a server/client platform

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default RMI Problems with doing calculations across a server/client platform

    Hi, ive been working on this problem for about 5 hours now and haven't found a way to resolve it. It's an RMI program that has to be able to return the mean, mode and median of a set of numbers.

    It's composed of 4 classes; a client, an implementation class (with the math), an Interface, and a server class.

    Here's all of the code
    Client
    import java.rmi.NotBoundException;
    import java.rmi.RMISecurityManager;
    import java.rmi.RemoteException;
    import java.rmi.registry.LocateRegistry;
    import java.rmi.registry.Registry;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Scanner;
    import java.io.*;
     
        public class rmiClient {
     
           public static void main (String[ ] args) throws RemoteException, NotBoundException {  
                if (System.getSecurityManager() == null) {
                    System.setSecurityManager(new RMISecurityManager());
                }
     
           String hostName = "localhost"; // default host name
           // assign host machine name to connect to
           if (args.length != 0) {
              if (args[0] != null) hostName = args[0]; // user specified machine
           }  
     
            try {   
                // connect to the remote object via the registry    
                Registry registry = LocateRegistry.getRegistry(hostName);
                rmiInterface stub = (rmiInterface) registry.lookup("myFunctions");
     
                Scanner rmiScanner = new Scanner(System.in);
                System.out.println( '\n' + "Enter the numbers to be calculated" + '\n');
     
                int [] values = new int [8]; 
                int math;
                int number=0;
     
                int breaker;
                int i = 0;
     
                while (true && i<8) {
                	breaker = rmiScanner.nextInt();
                		if (breaker <=0)
                			break;
                			values[i]= breaker;
                					i++;
                }
                String tempArray = Arrays.toString(values);
     
     
     
                System.out.println();
                System.out.println("Enter your Selection: ");
                System.out.println("1 - Return the mean");
                System.out.println("2 - Return the mode");
                System.out.println("3 - Return the median");
                System.out.println("4 - Exit");
     
                math = rmiScanner.nextInt(); //read client request
     
                int results;
                results=0;
                if (math==1) {
     
    					System.out.println("The mean is "+stub.mean(avg));
                    	} 
     
            } catch (Exception e) {System.out.println("Error : " + e); }
            System.exit(0);
           }
     
     
     
        }


    Implementation

    import java.rmi.RemoteException;
    import java.rmi.registry.LocateRegistry;
     
    public class rmiImpl  implements rmiInterface { 
     
     
    	int avg;
     
     
      // default constructor
      public rmiImpl() {
              super();
     } // end constructor 
     
      public int mean (int number){
       // calculates the mean of the numbers
    	  int[] values = new int [8];
    	  int i = 0;
    	  for (i=0; i< values.length; i++)
    	  {
    		  avg = avg + values[i];
    	  }
    	  return avg / values.length;
     
      }
     
    @Override
    public int mode(int mode) throws RemoteException {
    	// TODO Auto-generated method stub
    	return 0;
    }
     
    @Override
    public int median(int median) throws RemoteException {
    	// TODO Auto-generated method stub
    	return 0;
    }
     
      /*public int mode (int mode){
    	  int maxval = 0, maxCount = 0;
    	  for (int i = 0; i < values.length; i++) {
    		  int count = 0;
    		  for (int j = 0; j < values.length; ++j) {
    			  if (values[j] == values[i]) ++count;
    		  }
    		  if (count > maxCount) {
    			  maxCount = count;
    			  maxval = values[i];
    		  }
    	  }
    	  return maxval;
      }
     
      public int median (int median) {
    	  int mid = values.length/2;
    	  if (values.length%2 == 1) {
    		  return values[mid];
    	  } else {
    		  return (int) ((values[mid-1] + values[mid]) / 2.0);
    	  }
    	  }
    	  */
      }


    Interface

     
    import java.rmi.Remote;
    import java.rmi.RemoteException;
    import java.rmi.registry.LocateRegistry;
     
     
    public interface rmiInterface extends Remote {
     
    	 public int mean(int number) throws RemoteException;
     
    	 public int mode (int mode) throws RemoteException; 
     
    	 public int median (int median) throws RemoteException;
     
    }


    Server class

    import java.rmi.RMISecurityManager;
    import java.rmi.registry.LocateRegistry;
    import java.rmi.registry.Registry;
    import java.rmi.server.UnicastRemoteObject;
     
    	    public class Serverclass {
     
    	        public static void main (String args[]) {
    	            if (System.getSecurityManager() == null) {
    	                System.setSecurityManager(new RMISecurityManager());
    	            }       
     
    	            try {
    	                rmiImpl impl =  new rmiImpl();
    	                rmiInterface rmiStub = (rmiInterface) UnicastRemoteObject.exportObject(impl, 0);
     
    	                // Bind the remote object's stub in the registry
    	                Registry registry = LocateRegistry.getRegistry();
    	                registry.bind("myFunctions", rmiStub);
    	            }
    	            catch (Exception e) {System.out.println("Error: " + e); }
    	            System.out.println("Server started waiting ...");
    	        } // end main
     
    	    } //  end CensorServer


    If anyone could have a read over this i'd be hugely appreciative since i'm at a total loss at the moment.


    I figure that I need to use this excerpt from rmiClient

    if (math==1) {
     
    					System.out.println("The mean is "+stub.mean(avg));
                    	}

    to display the results of the calculation performed in the implementation class..


    Anyway hopefully someone can help,


    thanks


  2. #2
    Junior Member
    Join Date
    Mar 2014
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: RMI Problems with doing calculations across a server/client platform

    bump

Similar Threads

  1. Problems with sockets on simple server and client programs.
    By gongshow20 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 24th, 2013, 09:17 PM
  2. User input and calculations problems
    By javabeg123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 7th, 2011, 07:32 PM
  3. Problems With Client/Server Operations
    By bgroenks96 in forum Java Theory & Questions
    Replies: 2
    Last Post: July 26th, 2011, 03:48 PM
  4. can exceptions be propagated from server to client using java rmi?
    By slashdevslashnull in forum Java Networking
    Replies: 0
    Last Post: June 29th, 2011, 10:37 PM
  5. Problems with client/server application
    By lori in forum AWT / Java Swing
    Replies: 3
    Last Post: January 15th, 2010, 01:12 PM