SDSU CS 696 Emerging Technologies: Distributed Objects
Spring Semester, 1998
Errata HelloServer, Client Remotes, LocalRegistry, Serialization

To Lecture Notes Index
© 1998, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 21-Apr-98

Contents of Doc 13, Errata HelloServer, Client Remotes, LocalRegistry, Serialization

  1. An Improved HelloServer
  2. Error On Exporting Remotes from Client
  3. Running a Server without Running RMIRegistry
  4. Taking Control of Serialization
    1. Externalizable - A better way to take Control

Doc 13, Errata HelloServer, Client Remotes, LocalRegistry, Serialization Slide # 2

An Improved HelloServer

Improvements:
Use InetAddress to get local host name
sayHello does not throw RemoteException
package whitney.rmi.examples.basic;

import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.io.IOException;
import java.rmi.server.*;
import java.net.InetAddress;
import sdsu.util.ProgramProperties;
import sdsu.rmi.server.*;

public class HelloServer 
      extends UnicastRemoteObject
      implements Hello
   {

   public HelloServer() throws RemoteException 
      {
      }

   public String sayHello() 
      {
      return  "Hello World from " + getHostName();
      }

   protected static String getHostName()
      {
      try
         {
         return InetAddress.getLocalHost().getHostName();
         }
      catch (java.net.UnknownHostException who)
         {
         return "Unknown";
         }
      }

Doc 13, Errata HelloServer, Client Remotes, LocalRegistry, Serialization Slide # 3
    private static String getHelloHostAddress( String args[] ) 
      throws IOException
      {
      ProgramProperties flags = new ProgramProperties( args );
      String port = flags.getString( "port", "1099" );

      String host = flags.getString( "host", getHostName());
      
      return  "rmi://" + ":" + port + "/HelloServer";
      }
      
   public static void main(String args[])
      {
      // Create and install a security manager
      System.setSecurityManager(new RMISecurityManager());

      try 
         {
         String serverAddress = getHelloHostAddress( args );

         HelloServer serverObject = new HelloServer();
         
         Naming.rebind( serverAddress, serverObject);
      
         System.out.println("HelloServer bound in registry");
         } 
      catch (Exception e) 
         {
         System.out.println("HelloServer err: ");
         e.printStackTrace();
         }
      }
   }

Doc 13, Errata HelloServer, Client Remotes, LocalRegistry, Serialization Slide # 4

Error On Exporting Remotes from Client


On slide 21 of Doc 11, RMI Server Methods a comment in the code stated that the client needed to be run on the same machine as the rmiregistry

This is false.

The original has been corrected


Doc 13, Errata HelloServer, Client Remotes, LocalRegistry, Serialization Slide # 5

Running a Server without Running RMIRegistry


To run this example

1. Compile the Client, Server and the interface

2. Create the stub and skeletons by running rmic on the server

3. Move the client and stub to the client machine

4. Run the server

5. Run the client
The Interface
package whitney.rmi.examples.basic;

public interface Hello extends java.rmi.Remote 
   {
   String sayHello() throws java.rmi.RemoteException;
   }


Doc 13, Errata HelloServer, Client Remotes, LocalRegistry, Serialization Slide # 6
The Client
package whitney.rmi.examples.basic;

import java.rmi.*;
import java.net.MalformedURLException;
import java.io.IOException;
import java.rmi.server.*;
import sdsu.util.ProgramProperties;
import sdsu.rmi.server.*;

public class HelloClient 
   {
   public static void main(String args[]) 
      {
      try 
         {
         String server = getHelloHostAddress( args);

         Hello remote = (Hello) Naming.lookup( server );
         String message = remote.sayHello();
         System.out.println( message );
         } 
      catch ( Exception error) 
         {
         error.printStackTrace();
         }
      }

   private static String getHelloHostAddress( String args[] ) 
      throws IOException
      {
      ProgramProperties flags = new ProgramProperties( args );
      String host = flags.getString( "h" );
      String port = flags.getString( "p", "1099" );
      
      return "rmi://" + host + ":" + port + "/HelloServer";
      }
   }



Doc 13, Errata HelloServer, Client Remotes, LocalRegistry, Serialization Slide # 7
The Server
package whitney.rmi.examples.basic;

import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.UnicastRemoteObject;
import java.io.IOException;
import java.rmi.server.*;
import java.net.InetAddress;
import sdsu.util.ProgramProperties;

public class HelloServer 
      extends UnicastRemoteObject
      implements Hello
   {

   public HelloServer() throws RemoteException 
      { }

   public String sayHello() 
      {
      return  "Hello World from " + getHostName();
      }

   protected static String getHostName()
      {
      try
         {
         return InetAddress.getLocalHost().getHostName();
         }
      catch (java.net.UnknownHostException who)
         {
         return "Unknown";
         }
      }
      

Doc 13, Errata HelloServer, Client Remotes, LocalRegistry, Serialization Slide # 8
//The Important Part

   public static void main(String args[])
      {
      // Create and install a security manager
      System.setSecurityManager(new RMISecurityManager());
      
      try 
         {
         ProgramProperties flags = 
               new ProgramProperties( args );
         int port = flags.getInt( "p", 1099 );
         String serverLabel =  "HelloServer";

         HelloServer serverObject = new HelloServer();
         Registry localRegistry = 
            LocateRegistry.createRegistry(port);

         localRegistry.rebind( serverLabel, serverObject);
      
         System.out.println("HelloServer bound in registry");
         }

      catch (Exception e) 
         {
         System.out.println("HelloServer err: ");
         e.printStackTrace();
         }
      }
   }


Doc 13, Errata HelloServer, Client Remotes, LocalRegistry, Serialization Slide # 9

Taking Control of Serialization

class Roger implements Serializable
   {
   private int lowBid;
   private float averageBid;
   private int highBid;
   
   public Roger(int lowBid, int highBid )
      {
      this.lowBid = lowBid;
      this.highBid = highBid;
      averageBid = (lowBid + highBid)/2;
      }
   
   private void readObject( ObjectInputStream in) 
      throws IOException
      {
      lowBid = in.readInt();
      averageBid = in.readFloat();
      highBid = in.readInt();
      }

   private void writeObject( ObjectOutputStream out) 
      throws IOException
      {
      out.writeInt( lowBid );
      out.writeFloat( averageBid );
      out.writeInt( highBid );
      }
            
   public String toString()
      {
      return " " + lowBid + " " + averageBid;
      }
   }

Doc 13, Errata HelloServer, Client Remotes, LocalRegistry, Serialization Slide # 10

Externalizable - A better way to take Control

class Roger implements Externalizable
   {
   private int lowBid;
   private float averageBid;
   private int highBid;
   
   public Roger() {}; //Required
   
   public Roger(int lowBid, int highBid )
      {
      this.lowBid = lowBid;
      this.highBid = highBid;
      averageBid = (lowBid + highBid)/2;
      }
   
   public void readExternal( ObjectInput in) 
      throws IOException
      {
      lowBid = in.readInt();
      averageBid = in.readFloat();
      highBid = in.readInt();
      }

   public void writeExternal( ObjectOutput out) 
      throws IOException
      {
      out.writeInt( lowBid );
      out.writeFloat( averageBid );
      out.writeInt( highBid );
      }
            
   public String toString()
      { return " " + lowBid + " " + averageBid; }
   }


Doc 13, Errata HelloServer, Client Remotes, LocalRegistry, Serialization Slide # 11
Test Program for last two Examples
import java.io.*;

public class Test
   {
   public static void main( String args[] ) throws Exception 
      {
      serialize();
      deserialize();
      System.out.println( "Done" );
      }
      
   public static void serialize() throws Exception
      {
      OutputStream outputFile = 
            new FileOutputStream( "Serialized" );
      ObjectOutputStream cout = 
            new ObjectOutputStream( outputFile );
      cout.writeObject( new Roger( 1, 5));
      cout.close();
      }   
   
   public static void deserialize() throws Exception 
      {
      InputStream inputFile = 
         new FileInputStream( "Serialized" );
      ObjectInputStream cin = 
         new ObjectInputStream( inputFile );
      System.out.println( cin.readObject() );
      }
   }





visitors since 24-Feb-98