Archivo: Documento 1 no guardado import java.io.*; import java.net.*; import java.util.*; class IMServer extends Thread { Scanner in; PrintWriter out; static ArrayList
pool = new ArrayList(); 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 ) { for(PrintWriter output : pool) 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) {} pool.remove(out); out.close(); broadcast("CLIENT GONE"); } }
Página 1 de 1