How to Code RMI

For RMI theory, see rmi-theory.html

Make a Remote Interface

This interface should extend java.rmi.Remote.  Every function within it should throw java.rmi.RemoteException.  The functions together should capture the behavior of the service you want to perform.
 
public interface RemoteInterface extends java.rmi.Remote{
      public java.util.Date askTime() throws java.rmi.RemoteException;
      public void tellTime(java.util.Date d) throws java.rmi.RemoteException;
}

Make a Remote Object

This object should extend java.rmi.server.UnicastRemoteObject and implement the interface you created above.  Hopefully the functions you make here do the service you are trying to create.
 
public class RemoteObject extends java.rmi.server.UnicastRemoteObject
  implements RemoteInterface{
  public RemoteObject() throws java.rmi.RemoteException{
         // Empty constructor
  }
  public java.util.Date askTime() throws java.rmi.RemoteException{
    System.out.println("RemoteObject.askTime called" + new java.util.Date() + "\n");
    return new java.util.Date();
  }
  public void tellTime(java.util.Date d) throws java.rmi.RemoteException{
    System.out.println("RemoteObject.tellTime called" + d + "\n");
  }
}

Make a Server

The server should have a 'main' that ...
public class Server{
  public static void main(String[] args){
    try{
      System.setSecurityManager(new java.rmi.RMISecurityManager());
      RemoteInterface v=new RemoteObject();
      java.rmi.registry.LocateRegistry.createRegistry(5099);
      java.rmi.Naming.rebind("//:5099/count", v);
    }catch(java.rmi.UnknownHostException x){x.printStackTrace();}
     catch(java.rmi.RemoteException x){x.printStackTrace();}
     catch(java.net.MalformedURLException x){x.printStackTrace();}
     catch(Exception x){x.printStackTrace();}
  }
}

Make a Client

The client should
public class Client{
  public static void main(String[] args){
    RemoteInterface s=null;
    try{
      s=(RemoteInterface)java.rmi.Naming.lookup("rmi://euclid.nmu.edu:5099/count");
      s.tellTime(new java.util.Date());
      System.out.println(s.askTime());
    }catch (Exception x){x.printStackTrace();}
  }
}

Running it  

  See howto-start.html