//: c15:MultiJabberServer.java // From Thinking in Java, 2nd Edition // Available at http://www.BruceEckel.com // (c) Bruce Eckel 1999 // Copyright notice in Copyright.txt // A server that uses multithreading to handle // any number of clients. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.net.*; import javax.swing.Timer; class ServeOneJabber extends Thread { private Socket socket; private BufferedReader in; private PrintWriter out; private static int counter = 0; private int id = counter++; private String str = " "; private int umbral = 0; private Runtime runTime = Runtime.getRuntime(); private Process process = null; private BufferedReader inping; public ServeOneJabber(Socket s) throws IOException { socket = s; in = new BufferedReader( new InputStreamReader( socket.getInputStream())); // Enable auto-flush: out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( socket.getOutputStream())), true); // If any of the above calls throw an // exception, the caller is responsible for // closing the socket. Otherwise the thread // will close it. start(); // Calls run() } public void run() { // Timer timer1 = new Timer(5000, new ActionListener() { // public void actionPerformed(ActionEvent e) { // //// System.out.println(socket.getInetAddress().toString()); //// System.out.println(socket.getRemoteSocketAddress().toString()); //// System.out.println(socket.getInetAddress().toString().substring( //// 1,socket.getInetAddress().toString().length())); //// try { //// process = runTime.exec("ping "+socket.getInetAddress(). //// toString().substring( //// 1,socket.getInetAddress().toString().length()) //// ); //// inping = new BufferedReader( //// new InputStreamReader(process.getInputStream())); //// //// String aux = inping.readLine(); //// while (aux!=null) //// System.out.println (aux); //// aux = inping.readLine(); //// //// } catch (IOException e2) { } // // // }//actionEvent // }//actionlistener // ); timer1.start(); try { while (true) { if (str.equals("END")) break; str = in.readLine(); if (str.equals("Estableciendo Conexion ...")){ System.out.println("Conexion establecida con Cliente " + id ); out.println("Conexion establecida"); continue; } if (str.equals("Reciving Data...")){ out.println("id"); id = Integer.parseInt(in.readLine()); out.println("umbral"); umbral = Integer.parseInt(in.readLine()); System.out.println("Echoing: " + str + " a Cliente " + id); //out.println("Primer dato recibido/enviado con exito"); for(int i=umbral; i < 50; i++){ out.println(i); try{ Thread.sleep(200); //fija el temporizador }catch(java.lang.InterruptedException e){} if( (id == 0) && (i==20) ){ out.println("Change Sender"); out.println("200.1.17.195"); id++; socket.close(); // timer1.stop(); //str = "END"; break; } }//fin for if (str.equals("Reciving Data Nuevo Servidor...")) System.out.println(str + " Cliente " + id); }//fin if out.println("END"); }//fin del while System.out.println("closing Cliente "+ id + "..."); } catch (IOException e) { } finally { try { socket.close(); } catch(IOException e) {} } } } public class MultiJabberServer { static final int PORT = 9090; public static void main(String[] args) throws IOException { ServerSocket s = new ServerSocket(PORT); System.out.println("Server Started"); try { while(true) { // Blocks until a connection occurs: Socket socket = s.accept(); try { new ServeOneJabber(socket); } catch(IOException e) { // If it fails, close the socket, // otherwise the thread will close it: socket.close(); } } } finally { s.close(); } } } ///:~