// A server object that implements the NextNumber // remote interface import java.rmi.RemoteException; import java.rmi.RMISecurityManager; import java.rmi.Naming; import java.rmi.server.UnicastRemoteObject; import java.net.InetAddress; import java.net.UnknownHostException; import java.net.MalformedURLException; public class NextNumberServer extends UnicastRemoteObject implements NextNumber { public NextNumberServer() throws RemoteException { try { String host = InetAddress.getLocalHost().getHostName(); String url = "rmi://" + host + "/nextNumber"; Naming.rebind(url, this); System.out.println("\n Server bound to: " + url); } catch (UnknownHostException ex) { System.err.println("\n Couldn't get local host name"); System.exit(1); } catch (RemoteException ex) { System.err.println("\n Couldn't contact rmiregistry."); System.err.println(" Are you sure you're running rmiregistry?"); System.exit(1); } catch (MalformedURLException ex) { // Shouldn't happen System.exit(1); } } public int getNextNumber(int n) { return n+1; } public static void main(String[] args) { try { NextNumberServer server = new NextNumberServer(); } catch (RemoteException ex) { System.err.println("\n Trouble creating server: " + ex.getMessage()); ex.printStackTrace(); } } }