//: c15:MultiJabberClient.java // From Thinking in Java, 2nd Edition // Available at http://www.BruceEckel.com // (c) Bruce Eckel 1999 // Copyright notice in Copyright.txt // Client that tests the MultiJabberServer // by starting up multiple clients. import java.net.*; import java.io.*; import java.util.ArrayList; class JabberClientThread extends Thread { private Socket socket; private BufferedReader in; private PrintWriter out; private static int threadcount = 0; private ArrayList datos = new ArrayList(); private String str; private int id = 0; private int umbral = 0; public static int threadCount() { return threadcount; } private void lectorEscritor(){ try { in = new BufferedReader( new InputStreamReader( socket.getInputStream())); // Enable auto-flush: out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( socket.getOutputStream())), true); } catch(IOException e) { // The socket should be closed on any // failures other than the socket // constructor: try { socket.close(); } catch(IOException e2) {} } // Otherwise the socket will be closed by // the run() method of the thread. } public JabberClientThread(InetAddress addr) { System.out.println("Making client ..."); threadcount++; try { socket = new Socket(addr, MultiJabberServer.PORT); } catch(IOException e) { // If the creation of the socket fails, // nothing needs to be cleaned up. } lectorEscritor(); start(); } public void run() { try { out.println("Estableciendo Conexion ..."); System.out.println("Estableciendo Conexion ..."); str = in.readLine(); if (str.equals("Conexion establecida")){ System.out.println(str); } out.println("Reciving Data..."); if (str.equals("id")){ id = Integer.parseInt(in.readLine()); System.out.println(id); } while (true) { str = in.readLine(); System.out.println(str); datos.add(str); if (str.equals("id")){ out.println(id); } if (str.equals("umbral")){ out.println(umbral); } if (str.equals("Change Sender")){ str = in.readLine(); System.out.println(str); socket = new Socket(InetAddress.getByName(str), MultiJabberServer.PORT); lectorEscritor(); id++; umbral=25; out.println("Reciving Data..."); continue; } if (str.equals("END")) break; }//fin while(true) System.out.println(datos.toString()); out.println("END"); } catch(IOException e) { } finally { // Always close it: try { socket.close(); } catch(IOException e) {} threadcount--; // Ending this thread } } } public class MultiJabberClient { public static void main(String[] args) throws IOException, InterruptedException { // InetAddress addr = InetAddress.getByName(null); new JabberClientThread(InetAddress.getByName("200.1.18.165")); } }