andersonalmada2

Untitled

Sep 13th, 2022
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. package teste;
  2.  
  3. import java.io.IOException;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6. import java.util.Scanner;
  7.  
  8. public class Server implements Runnable {
  9.     public Socket cliente;
  10.  
  11.     public Server(Socket cliente) {
  12.         this.cliente = cliente;
  13.     }
  14.  
  15.     public static void main(String[] args) throws IOException {
  16.  
  17.         ServerSocket servidor = new ServerSocket(12345);
  18.         System.out.println("Porta 12345 aberta!");
  19.  
  20.         System.out.println("Aguardando conexão do cliente...");
  21.  
  22.         while (true) {
  23.             Socket cliente = servidor.accept();
  24.             Server tratamento = new Server(cliente);
  25.             Thread t = new Thread(tratamento);
  26.             t.start();
  27.         }
  28.     }
  29.  
  30.     @Override
  31.     public void run() {
  32.         System.out.println("Nova conexao com o cliente " + this.cliente.getInetAddress().getHostAddress());
  33.  
  34.         try {
  35.             Scanner s = null;
  36.             s = new Scanner(this.cliente.getInputStream());
  37.  
  38.             while (s.hasNextLine()) {
  39.                 System.out.println(s.nextLine());
  40.             }
  41.  
  42.             s.close();
  43.             this.cliente.close();
  44.         } catch (IOException e) {
  45.             e.printStackTrace();
  46.         }
  47.     }
  48. }
Add Comment
Please, Sign In to add comment