andersonalmada2

Untitled

Sep 13th, 2022
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. package teste;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintStream;
  5. import java.net.Socket;
  6. import java.net.UnknownHostException;
  7. import java.util.Scanner;
  8.  
  9. public class Client implements Runnable {
  10.  
  11.     private Socket cliente;
  12.  
  13.     public Client(Socket cliente) {
  14.         this.cliente = cliente;
  15.     }
  16.  
  17.     public static void main(String args[]) throws UnknownHostException, IOException {
  18.  
  19.         Socket socket = new Socket("127.0.0.1", 12345);
  20.  
  21.         Client c = new Client(socket);
  22.         Thread t = new Thread(c);
  23.         t.start();
  24.     }
  25.  
  26.     public void run() {
  27.         try {
  28.             PrintStream saida;
  29.             System.out.println("O cliente conectou ao servidor");
  30.  
  31.             Scanner teclado = new Scanner(System.in);
  32.  
  33.             saida = new PrintStream(this.cliente.getOutputStream());
  34.  
  35.             while (teclado.hasNextLine()) {
  36.                 String input = teclado.nextLine();
  37.                 saida.println(input);
  38.  
  39.                 if (input.equals(" ")) {
  40.                     break;
  41.                 }
  42.             }
  43.  
  44.             saida.close();
  45.             teclado.close();
  46.             this.cliente.close();
  47.             System.out.println("Fim do cliente!");
  48.         } catch (IOException e) {
  49.             e.printStackTrace();
  50.         }
  51.     }
  52. }
Add Comment
Please, Sign In to add comment