Source of BroadcastEchoServer.java


  1: 
  2: import java.util.*;
  3: import java.io.*;
  4: import java.net.*; 
  5: 
  6: class BroadcastClientHandler extends Thread {
  7: 
  8:   protected Socket incoming; 
  9:   protected int id; 
 10:   protected BufferedReader in; 
 11:   protected PrintWriter out;
 12:  
 13:   public BroadcastClientHandler(Socket incoming, int id) {
 14:     this.incoming = incoming; 
 15:     this.id = id; 
 16:     try {
 17:       if (incoming != null) {
 18:         in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));             
 19:         out = new PrintWriter(new OutputStreamWriter(incoming.getOutputStream()));             
 20:       }
 21:     } catch (Exception e) {
 22:       System.out.println("Error: " + e); 
 23:     }
 24:   }
 25: 
 26:   public synchronized void putMessage(String msg) {
 27:     if (out != null) {
 28:       out.println(msg); 
 29:       out.flush();
 30:     }
 31:   }
 32: 
 33:   public void run() {
 34:     System.out.println("Client handler " + id + " started."); 
 35:     if (in != null && 
 36:         out != null) {
 37:       putMessage("Hello! This is Java BroadcastEchoServer. Enter BYE to exit.");       
 38:       try {
 39:         for (;;) {
 40:           String str = in.readLine(); 
 41:           if (str == null) {
 42:             break; 
 43:           } else {
 44:             putMessage("Echo: " + str); 
 45:             System.out.println("Received (" + id + "): " + str);
 46: 
 47:             if (str.trim().equals("BYE")) {
 48:               break; 
 49:             } else {
 50:               Enumeration enum = BroadcastEchoServer.activeThreads.elements(); 
 51:               while  (enum.hasMoreElements()) {
 52:                 BroadcastClientHandler t =         
 53:                     (BroadcastClientHandler) enum.nextElement(); 
 54:                 if (t != this) {
 55:                   t.putMessage("Broadcast(" + id + "): " + str); 
 56:                 }
 57:               }
 58:             }
 59:           }
 60:         }
 61:         incoming.close(); 
 62:         BroadcastEchoServer.activeThreads.removeElement(this); 
 63:       } catch (IOException e) {} 
 64:     }
 65:     System.out.println("Client thread " + id + " stopped."); 
 66:   }
 67: 
 68: }
 69: 
 70: public class BroadcastEchoServer {
 71: 
 72:   static protected Vector activeThreads; 
 73: 
 74:   public static void main(String[] args) {
 75:     System.out.println("BroadcastEchoServer started."); 
 76:     activeThreads = new Vector(); 
 77:     int i = 1; 
 78:     try {
 79:       ServerSocket s = new ServerSocket(8010); 
 80:       for (;;) {
 81:         Socket incoming = s.accept(); 
 82:         System.out.println("Spawning client thread " + i);
 83:         BroadcastClientHandler newThread = 
 84:           new BroadcastClientHandler(incoming, i); 
 85:         activeThreads.addElement(newThread); 
 86:         newThread.start(); 
 87:         i++; 
 88:       }
 89:     } catch (Exception e) {
 90:       System.out.println("Error: " + e); 
 91:     }
 92: 
 93:     System.out.println("BroadcastEchoServer stopped."); 
 94:   }
 95: }
 96: