import java.net.*; import java.io.*; import java.util.*; public class BroadcastServer extends Thread { protected static boolean serverContinue = true; protected Socket clientSocket; protected Shared outputStreamList; public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; Shared s = new Shared (); try { serverSocket = new ServerSocket(10008); System.out.println ("Connection Socket Created"); try { while (serverContinue) { serverSocket.setSoTimeout(10000); //System.out.println ("Waiting for Connection"); try { new BroadcastServer (serverSocket.accept(), s); } catch (SocketTimeoutException ste) { //System.out.println ("Timeout Occurred"); } } } catch (IOException e) { System.err.println("Accept failed."); System.exit(1); } } catch (IOException e) { System.err.println("Could not listen on port: 10008."); System.exit(1); } finally { try { System.out.println ("Closing Server Connection Socket"); serverSocket.close(); } catch (IOException e) { System.err.println("Could not close port: 10008."); System.exit(1); } } } private BroadcastServer (Socket clientSoc, Shared s) { clientSocket = clientSoc; outputStreamList = s; start(); } public void run() { System.out.println ("New Communication Thread Started"); try { PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); outputStreamList.addStream (out); BufferedReader in = new BufferedReader( new InputStreamReader( clientSocket.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println ("Server: " + inputLine); if (inputLine.equals("?")) inputLine = new String ("\"Bye.\" ends Client, " + "\"End Server.\" ends Server"); out.println(inputLine); outputStreamList.sendMessageToStreams(inputLine); if (inputLine.equals("Bye.")) break; if (inputLine.equals("End Server.")) serverContinue = false; } // remove the stream from the shared data outputStreamList.removeStream (out); out.close(); in.close(); clientSocket.close(); } catch (IOException e) { System.err.println("Problem with Communication Server"); System.exit(1); } } } class Shared { private Vector outputStreamList; public Shared() { outputStreamList = new Vector(); } synchronized void addStream (PrintWriter outStream) { if (!(outputStreamList.contains(outStream))) { outputStreamList.addElement(outStream); System.out.println("Registered new client "); } // end if } synchronized void sendMessageToStreams (String s) { // Loop for streams in the vector and write s to each stream System.out.println( "**************************************\n" + "Broadcast initiated ---"); for (int i = 0; i < outputStreamList.size(); i++){ System.out.println("doing "+ i +"-th callback\n"); // convert the vector object to a callback object PrintWriter outStream = (PrintWriter)outputStreamList.elementAt(i); // invoke the callback method outStream.println(s); }// end for System.out.println("********************************\n" + "Server completed Broadcast ---"); } synchronized void removeStream (PrintWriter outStream) { // remove the stream from the vector if (outputStreamList.removeElement(outStream)) { System.out.println("Unregistered client "); } else { System.out.println("Unregister: client wasn't registered."); } } }