/*
 * EchoClient.java
 * Author: Sergio "Serabe" Rodríguez Arbeo
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.net.*;
import java.io.*;
import java.util.*;

public class EchoClient{

				private static int limite = 100;

				public static final void main(String[] main){
								
								//Lanza __limite__ hilos.
								
								for(int i=0; i<EchoClient.limite; i++) new EchoClientThread();
				}
}

class EchoClientThread extends Thread{

				public EchoClientThread(){
								
								//Llama al constructor padre y empieza a correr el hilo.
								
								super();
								this.start();
				}

				public void run(){

								/*
								 * Hace llamadas consecutivas al método conectar separadas
								 * por un intervalo aleatorio de segundos.
								 */
								
									while(true){
													conectar();
									}
				}

				public void conectar(){
								int port = 7777;
								String server = "localhost";
								try{
												
												//Se conecta al servidor
												
												Socket s = new Socket(server,port);
												
												//Captura los flujos de entrada/salida
												
												BufferedReader entrada = new BufferedReader(new InputStreamReader(s.getInputStream()));
												PrintWriter salida = new PrintWriter(new OutputStreamWriter(s.getOutputStream()),true);
												
												//Genera un mensaje aleatorio y lo envía
												
												String mensaje = Mensaje.aleatorio();
												salida.println(mensaje);

												//Recoge la entrada y comprueba que coincide
												//con lo enviado.
												
												String cadena = entrada.readLine();
												System.out.println(cadena.equals(mensaje));

												//Cierra el socket.
												
												s.close();

								//Maneja las excepciones
								
								}catch(UnknownHostException e){
												System.out.println("Servidor desconocido.\n"+e.toString());
								}catch(IOException e){
												System.out.println("Error de I/O.\n"+e.toString());
								}catch(Exception e){
												System.out.println(e.toString());
								}
				}
}

class Mensaje{

				//Genera un mensaje aleatorio.
				
				public static String aleatorio(){
								String[] mensajes = {"Hola, Mundo.","Melón.","Larga y próspera vida.","La Quinta Raza.","So long, and thanks for all the fish.","Spain is different.", "Com va això?","Bona tarda."};
								return mensajes[ (int) Math.round(Math.floor(Math.random() * mensajes.length)) ];
				}
}

