Archivo: /home/misan/workplace/IMServer/IMServer.
java Página 1 de 1
import java.io.*;
import java.net.*;
import java.util.*;
class IMServer extends Thread {
Scanner in;
PrintWriter out;
static ArrayList<PrintWriter> pool = new ArrayList<PrintWriter>();
public IMServer(Socket s) throws IOException {
in = new Scanner(s.getInputStream());
out = new PrintWriter(s.getOutputStream(),true);
pool.add(out);
start();
}
public static void main(String args[]) throws IOException {
ServerSocket ss = new ServerSocket(7777);
while(true) new IMServer(ss.accept());
}
public void broadcast(String l ) {
synchronized (pool) { for(PrintWriter output : pool) // NEW HERE
if ( !output.equals(out) ) output.println(l); }
}
public void run() {
broadcast("NEW CLIENT");
try {
while(true) {
String line = in.nextLine();
if (line.equalsIgnoreCase("quit")) break; else broadcast(line);
}
} catch (Exception e) {System.out.println("Exception "+e);}
synchronized (pool) {pool.remove(out);} // NEW HERE
out.close();
broadcast("CLIENT GONE");
}
}