public class SSLServer extends UnicastRemoteObject implements SSLServerInterface { private static final long serialVersionUID = 1L; public SSLServer() throws RemoteException { super(Registry.REGISTRY_PORT+1, new SslRMIClientSocketFactory(), new SslRMIServerSocketFactory()); //super(); } public String getMessage(String s) throws RemoteException { return s + " bar"; } }
public class serverStarter { public static void main(String[] args) { SSLServer sslServer = null; Registry sslRegistry = null; try { sslServer = new SSLServer(); } catch(RemoteException e) { System.out.println("Error: Cannot create SSLServer: RemoteException."); } System.out.println("Success: SSLServer created."); // Create security manager if(System.getSecurityManager()==null) { System.setSecurityManager(new RMISecurityManager()); } System.out.println("Success: Security manager set."); // Create sslRegistry try { sslRegistry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT, new SslRMIClientSocketFactory(), new SslRMIServerSocketFactory()); //sslRegistry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT); } catch (RemoteException e) { System.out.println("Error: Cannot create SSLRegistry: RemoteException."); } System.out.println("Success: SSLRegistry created."); // Bind sslServer to registry try { sslRegistry.bind("SSLSERVER", sslServer); } catch (AccessException e) { System.out.println("Error: Cannot bind SSLServer: AccessException"); System.exit(0); } catch (RemoteException e) { System.out.println("Error: Cannot bind SSLServer : RemoteException"); System.exit(0); } catch (AlreadyBoundException e) { System.out.println("Error: Cannot bind SSLServer: AlreadBoundException."); System.exit(0); } System.out.println("Success: SSLServer bound."); } }
public class SSLClient { private SSLServerInterface sslServer = null; private Registry sslRegistry = null; public SSLClient() { } public boolean connect(String ip, String name) { try { sslRegistry = LocateRegistry.getRegistry("localhost", Registry.REGISTRY_PORT); sslServer = (SSLServerInterface)sslRegistry.lookup(name); } catch (RemoteException e) { System.out.println("Client: RemoteException: connect()"); return false; } catch (NotBoundException e) { System.out.println("Client: NotBoundException: connect()"); return false; } return true; } public String getMessageFromServer(String msg) { String result = null; try { result = sslServer.getMessage(msg); } catch (RemoteException e) { return "Error: getMessageFromServer: RemoteException."; } return result; } }
public class clientStarter { public static void main(String args[]) { SSLClient c = new SSLClient(); String msg = "foo"; if(c.connect("localhost", "SSLSERVER")==false) { System.out.println("Error client connect"); System.exit(0); } System.out.println("Client sending: " + msg); msg = c.getMessageFromServer(msg); System.out.println("Client received: " + msg); } }