FileDocCategorySizeDatePackage
VerySimpleChatServer.javaAPI DocExample2269Thu Mar 24 10:58:12 GMT 2005None

VerySimpleChatServer

public class VerySimpleChatServer extends Object

Fields Summary
ArrayList
clientOutputStreams
Constructors Summary
Methods Summary
public voidgo()

      clientOutputStreams = new ArrayList<PrintWriter>();

       try {
       ServerSocket serverSock = new ServerSocket(5000);

       while(true) {
          Socket clientSocket = serverSock.accept();
          PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());         
          clientOutputStreams.add(writer);

       Thread t = new Thread(new ClientHandler(clientSocket));
       t.start();


       System.out.println("got a connection");
     }
       // now if I get here I have a connection
               
      }catch(Exception ex) {
         ex.printStackTrace();
      }
   
public static voidmain(java.lang.String[] args)

         new VerySimpleChatServer().go();
    
public voidtellEveryone(java.lang.String message)

      Iterator it = clientOutputStreams.iterator();
      while(it.hasNext()) {
        try {
           PrintWriter writer = (PrintWriter) it.next();
           writer.println(message);
           writer.flush();
         } catch(Exception ex) {
              ex.printStackTrace();
         }
      
       } // end while