Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @author Lencina, Arduino, Cavalieri
- */
- import java.io.*;
- import java.net.*;
- import java.util.*;
- /**
- *
- * Clase cliente. Pide que el puerto al que desea conectarse para poder ingresar a la sala de chat
- */
- public class Clientes extends Thread {
- private static Socket cliente = null;
- private static DataOutputStream os = null;
- private static DataInputStream is = null;
- private static Scanner inp = new Scanner(System.in);
- private static boolean closed = false;
- public static void main(String[] args) throws IOException {
- BufferedReader teclado = new BufferedReader(new InputStreamReader(System.in));
- int puerto;
- String host;
- inp.useDelimiter("\n");
- if (args.length < 2) {
- //System.out.print("Ingrese el host: ");// Si quieren cambiar el host solo hay que descomentar estas lineas
- host = "localhost";//teclado.readLie();
- System.out.print("Ingrese el puerto: ");
- String xx = teclado.readLine();
- puerto = Integer.parseInt(xx);
- } else {
- host = args[0];
- puerto = Integer.parseInt(args[1]);
- }
- System.out.println("Intentando conectarse al host: " + host + " en el puerto " + puerto);
- //Abro la conexion
- try {
- cliente = new Socket(host, puerto);
- os = new DataOutputStream(cliente.getOutputStream());
- is = new DataInputStream(cliente.getInputStream());
- } catch (UnknownHostException e) {
- System.err.println("No es posible conectarse al host " + host);
- } catch (IOException e) {
- System.err.println("No es posible obtener conexion de I/O con el " + host);
- }
- if (cliente != null && os != null && is != null) {
- try {
- System.out.println("\nConectado al host: " + host + " en el puerto " + puerto+"\n");
- /* Creo un hilo para leer desde el servidor */
- new Thread(new Clientes()).start();
- while (!closed) {
- os.writeUTF(inp.next().trim());
- }
- /*
- * Cierro los stream y el socket
- */
- os.close();
- is.close();
- cliente.close();
- } catch (IOException e) {
- System.err.println("IOException: " + e);
- }
- }
- }
- @Override
- public void run() {
- /*
- * Queda recibiendo del servidor hasta que este responde Adios
- */
- String responseLine;
- try {
- while ((responseLine = is.readUTF()) != null) {
- System.out.println(responseLine);
- if (responseLine.contains("***Adios")) {
- break;
- }
- }
- closed = true;
- } catch (IOException e) {
- System.err.println("IOException: " + e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement