Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package teste;
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.Scanner;
- public class Server implements Runnable {
- public Socket cliente;
- public Server(Socket cliente) {
- this.cliente = cliente;
- }
- public static void main(String[] args) throws IOException {
- ServerSocket servidor = new ServerSocket(12345);
- System.out.println("Porta 12345 aberta!");
- System.out.println("Aguardando conexão do cliente...");
- while (true) {
- Socket cliente = servidor.accept();
- Server tratamento = new Server(cliente);
- Thread t = new Thread(tratamento);
- t.start();
- }
- }
- @Override
- public void run() {
- System.out.println("Nova conexao com o cliente " + this.cliente.getInetAddress().getHostAddress());
- try {
- Scanner s = null;
- s = new Scanner(this.cliente.getInputStream());
- while (s.hasNextLine()) {
- System.out.println(s.nextLine());
- }
- s.close();
- this.cliente.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment