//@author Rhandi Martin import java.io.*; import java.net.*; public class ServerThread extends Thread { private ThreadGroup parent; private static ThreadGroup st; private Socket cSocket; private PrintWriter pw; private BufferedReader br; private String name; public ServerThread(ThreadGroup children, String name, Socket cSocket) { super(children,name); this.name = name; this.cSocket = cSocket; this.st = children; } //end ctor(ThreadGroup,String,Socket) public void run() { String input="",output=""; try { br = new BufferedReader(new InputStreamReader(cSocket.getInputStream())); pw = new PrintWriter(cSocket.getOutputStream(),true); while((input = br.readLine())!=null) { if(input!=null) { input=input.trim(); if (!(input.equals(""))) { if(input.equals("/quit")) { output = name+" has parted the channel."; System.out.println(output); writeOut(output); pw.println("Goodbye."); break; } else { output = name+": "+input; System.out.println(output); writeOut(output); } //end else }//end if } //end if } //end while br.close(); pw.close(); } catch(IOException ioe) { System.err.printf("Unable to establish connection to %s - Exiting\n",name); } catch(Exception e) { System.err.printf("Unable to establish connection to %s - Exiting\n",name); } //end t-c block } //end method - void init() private void writeOut(String output) { ThreadGroup tg = this.getThreadGroup(); Thread[] peers = new Thread[tg.activeCount()]; tg.enumerate(peers); for(Thread t: peers) { try { if(!(((ServerThread)t).equals(this))) { ((ServerThread)t).write(output); } //end if }catch(ClassCastException cce) { continue; //Do nothing } //end t-c block } //end for } //end method - void writeOut(String) synchronized void write(String output) { pw.println(output); } //end method - void write(String) } //end main