// Fortune server import java.io.*; import java.rmi.*; import java.rmi.server.*; import java.rmi.registry.LocateRegistry; public class FortuneServer extends UnicastRemoteObject implements Fortune { public FortuneServer() throws java.rmi.RemoteException { super(); } public String getFortune() throws RemoteException, ServerNotActiveException { // ToDo: JNI tcp_wrappers hosts.allow restrictions for ClientHost // System.out.println( "FortuneServer: getFortune() called from " + this.getClientHost()); // should really invoke /usr/local/bin/fortune to get random fortune... // return "Try not to have a good time ... This is supposed to be educational.\n -- Charles Schulz"; } public static void createPortFile( int port) throws java.io.IOException { PrintWriter fout = new PrintWriter( new BufferedWriter( new FileWriter( "port"))); fout.println( port); if( fout.checkError()) { System.out.println( "FortuneServer: error writing port file."); System.exit(1); } fout.close(); } public static void main(String args[]) throws Exception { int port = 21099; if (args.length == 1) port = Integer.parseInt(args[0]); // Create and install the security manager // // skip to avoid creating a security policy file for JDK 1.2 - System.setSecurityManager(new RMISecurityManager()); try { LocateRegistry.createRegistry(port); Fortune f = new FortuneServer(); Naming.rebind("rmi://:" + port + "/FortuneServer", f); System.out.println("FortuneServer: created and bound in the registry on port " + port); } catch (Exception e) { System.out.println("FortuneServer: " + e); System.exit(1); } createPortFile( port); } }