Advertisement
Guest User

ClienteSocket

a guest
Oct 1st, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. /**
  2.  *
  3.  * @author Lencina, Arduino, Cavalieri
  4.  */
  5. import java.io.*;
  6. import java.net.*;
  7. import java.util.*;
  8.  
  9. /**
  10.  *
  11.  * Clase cliente. Pide que el puerto al que desea conectarse para poder ingresar a la sala de chat
  12.  */
  13. public class Clientes extends Thread {
  14.  
  15.   private static Socket cliente = null;
  16.   private static DataOutputStream os = null;
  17.   private static DataInputStream is = null;
  18.   private static Scanner inp = new Scanner(System.in);
  19.   private static boolean closed = false;
  20.  
  21.   public static void main(String[] args) throws IOException {
  22.       BufferedReader teclado = new BufferedReader(new InputStreamReader(System.in));
  23.     int puerto;
  24.     String host;
  25.     inp.useDelimiter("\n");
  26.     if (args.length < 2) {
  27.       //System.out.print("Ingrese el host: ");// Si quieren cambiar el host solo hay que descomentar estas lineas
  28.       host = "localhost";//teclado.readLie();
  29.       System.out.print("Ingrese el puerto: ");
  30.       String xx = teclado.readLine();
  31.       puerto = Integer.parseInt(xx);
  32.     } else {
  33.       host = args[0];
  34.       puerto = Integer.parseInt(args[1]);
  35.     }
  36.     System.out.println("Intentando conectarse al host: " + host + " en el puerto " + puerto);
  37.     //Abro la conexion
  38.     try {
  39.       cliente = new Socket(host, puerto);
  40.       os = new DataOutputStream(cliente.getOutputStream());
  41.       is = new DataInputStream(cliente.getInputStream());
  42.     } catch (UnknownHostException e) {
  43.       System.err.println("No es posible conectarse al host " + host);
  44.     } catch (IOException e) {
  45.       System.err.println("No es posible obtener conexion de I/O con el " + host);
  46.     }
  47.     if (cliente != null && os != null && is != null) {
  48.       try {
  49.         System.out.println("\nConectado al host: " + host + " en el puerto " + puerto+"\n");
  50.         /* Creo un hilo para leer desde el servidor */
  51.         new Thread(new Clientes()).start();
  52.         while (!closed) {
  53.           os.writeUTF(inp.next().trim());
  54.         }
  55.         /*
  56.          * Cierro los stream y el socket
  57.          */
  58.         os.close();
  59.         is.close();
  60.         cliente.close();
  61.       } catch (IOException e) {
  62.         System.err.println("IOException:  " + e);
  63.       }
  64.     }
  65.   }
  66.  
  67.   @Override
  68.   public void run() {
  69.     /*
  70.      * Queda recibiendo del servidor hasta que este responde Adios
  71.      */
  72.     String responseLine;
  73.     try {
  74.       while ((responseLine = is.readUTF()) != null) {
  75.         System.out.println(responseLine);
  76.         if (responseLine.contains("***Adios")) {
  77.           break;
  78.         }
  79.       }
  80.       closed = true;
  81.     } catch (IOException e) {
  82.       System.err.println("IOException:  " + e);
  83.     }
  84.   }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement