Quick assignment( 2 Questions) need help

profilejontha11
week6.zip

HelloClient.java

HelloClient.java

import  java . io . * ;
import  java . rmi . * ;

/**
 * This class represents the object client for a distributed
 * object of class Hello, which implements the remote interface
 * HelloInterface.
 *  @author  M. L. Liu
 */

public   class   HelloClient  
{
    public   static   void  main ( String  args [])  
    {
       try  
       {
          int   RMIPort ;          
          String  hostName ;
          InputStreamReader  is  =   new   InputStreamReader ( System . in );
          BufferedReader  br  =   new   BufferedReader ( is );
          System . out . println ( "Enter the RMIRegistry host name:" );
         hostName  =  br . readLine ();
          System . out . println ( "Enter the RMIregistry port number:" );
          String  portNum  =  br . readLine ();
          RMIPort   =   Integer . parseInt ( portNum );
          String  registryURL  =  
             "rmi://"   +  hostName +   ":"   +  portNum  +   "/hello" ;   
          // find the remote object and cast it to an interface object
          HelloInterface  h  =
            ( HelloInterface ) Naming . lookup ( registryURL );
          System . out . println ( "Lookup completed "   );
         
 
          // invoke the remote method
          String  message  =  h . sayHello ( "Donald Duck" );    
          System . out . println ( "HelloClient: "   +  message );
       }   // end try 
       catch   ( Exception  e )  
       {
          System . out . println ( "Exception in HelloClient: "   +  e );
       }   // end catch
    }   //end main
} //end class

HelloImpl.java

HelloImpl.java

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

/**
 * This class implements the remote interface 
 *    HelloInterface.
 *  @author  M. L. Liu
 */

public   class   HelloImpl   extends   UnicastRemoteObject   implements   HelloInterface  
      {

     public   HelloImpl ()   throws   RemoteException
     {
         super (   );
     }
  
     public   String  sayHello ( String  name )   throws   RemoteException  
     {
           return   "Hello, World!"   +  name ;
     }
}   // end class

HelloInterface.java

HelloInterface.java

// A simple RMI interface file - M. Liu
import  java . rmi . * ;

/**
 * This is a remote interface.
 *  @author  M. L. Liu
 */

public   interface   HelloInterface   extends   Remote  
{
/**
 * This remote method returns a message.
 *  @param   name - a String containing a name.
 *  @return  a String message.
 */
    public   String  sayHello ( String  name )  
       throws  java . rmi . RemoteException ;
      
 
}   //end interface

HelloServer.java

HelloServer.java

import  java . rmi . * ;
import  java . rmi . server . * ;
import  java . rmi . registry . Registry ;
import  java . rmi . registry . LocateRegistry ;
import  java . net . * ;
import  java . io . * ;

/**
 * This class represents the object server for a distributed
 * object of class Hello, which implements the remote interface
 * HelloInterface.
 *  @author  M. L. Liu
 */

public   class   HelloServer   
{
    public   static   void  main ( String  args [])  
    {
       InputStreamReader  is  =   new   InputStreamReader ( System . in );
       BufferedReader  br  =   new   BufferedReader ( is );
       String  portNum ,  registryURL ;
       try
       {      
          System . out . println ( "Enter the RMIregistry port number:" );
         portNum  =   ( br . readLine ()). trim ();
          int   RMIPortNum   =   Integer . parseInt ( portNum );
         startRegistry ( RMIPortNum );
          HelloImpl  exportedObj  =   new   HelloImpl ();
         registryURL  =   "rmi://localhost:"   +  portNum  +   "/hello" ;
          Naming . rebind ( registryURL ,  exportedObj );
  /**/          System . out . println
  /**/         ( "Server registered.  Registry currently contains:" );
  /**/          // list names currently in the registry
  /**/         listRegistry ( registryURL );  
          System . out . println ( "Hello Server ready." );
       } // end try
       catch   ( Exception  re )  
       {
          System . out . println ( "Exception in HelloServer.main: "   +  re );
       }   // end catch
   }   // end main

    // This method starts a RMI registry on the local host, if it
    // does not already exists at the specified port number.
    private   static   void  startRegistry ( int   RMIPortNum )
       throws   RemoteException {
       try  
       {
          Registry  registry  =   LocateRegistry . getRegistry ( RMIPortNum );
         registry . list (   );    // This call will throw an exception
                             // if the registry does not already exist
       }
       catch   ( RemoteException  e )  
       {  
          // No valid registry at that port.
  /**/           System . out . println
  /**/           ( "RMI registry cannot be located at port "   +   RMIPortNum );
  /**/         Registry  registry  =   LocateRegistry . createRegistry ( RMIPortNum );
  /**/          System . out . println (
  /**/   "RMI registry created at port "   +   RMIPortNum );
       }
    }   // end startRegistry

   // This method lists the names registered with a Registry object
   private   static   void  listRegistry ( String  registryURL )   throws   RemoteException ,   MalformedURLException  
   {
        System . out . println ( "Registry "   +  registryURL  +   " contains: " );
        String   [   ]  names  =   Naming . list ( registryURL );
        for   ( int  i = 0 ;  i  <  names . length ;  i ++ )
           System . out . println ( names [ i ]);
   }   //end listRegistry
     
}   // end class

IMAG0175.jpg

IMAG0176.jpg

Archive created by free jZip.url

[InternetShortcut] URL=http://www.jzip.com/archive_link