//package examples.hello; import java.rmi.*; import java.rmi.server.UnicastRemoteObject; import java.net.*; import java.util.*; import java.lang.*; public class HelloImpl extends UnicastRemoteObject implements Hello { private int counter = -1; ArrayList clients; public HelloImpl() throws RemoteException { // The superclass constrctor exports the interface and gets a port super(); clients = new ArrayList(); } public synchronized void send(String str) throws RemoteException{ System.out.println("I am supposed to be sending: " + str); Iterator itt = clients.iterator(); Player player; while(itt.hasNext()) { player = (Player) itt.next(); try { player.send(str); } catch(RemoteException e) { System.out.println("Removing player..."); clients.remove(player); }; }; }; public synchronized void register(Player player) throws RemoteException { player.send("you are registered"); clients.add(player); }; public synchronized String sayHello() throws RemoteException { // This is the "service" provided. System.out.println("Serving new request..."); counter++; return "Hello World! " + counter; } public static void main(String args[]) { // Create and install a security manager System.setSecurityManager(new RMISecurityManager()); try { // Construct the server object. HelloImpl obj = new HelloImpl(); // Register the server with the name server. Naming.rebind("HelloServer", obj); } catch (RemoteException e) { System.out.println("HelloApplet RemoteException caught"); } catch (MalformedURLException e) { System.out.println("HelloApplet MalformedURLException caught"); } } };