-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServidorChat.java
More file actions
44 lines (29 loc) · 1.37 KB
/
ServidorChat.java
File metadata and controls
44 lines (29 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ServidorChat {
public static void main(String[] args) {
int porta = 12345;
ExecutorService poolDeThreads = Executors.newFixedThreadPool(50);
System.out.println("--- Servidor de Chat Iniciado ---");
System.out.println("Aguardando conexoes na porta " + porta + "...");
GerenciadorDeClientes gerenciador = new GerenciadorDeClientes();
try (ServerSocket serverSocket = new ServerSocket(porta)) {
while (true) {
Socket socketDoCliente = serverSocket.accept();
System.out.println("Novo cliente conectado: " + socketDoCliente.getRemoteSocketAddress());
AtendenteCliente atendente = new AtendenteCliente(socketDoCliente, gerenciador);
poolDeThreads.execute(atendente);
}
} catch (IOException e) {
System.out.println("Erro no Servidor: " + e.getMessage());
} finally {
if (poolDeThreads != null) {
System.out.println("Desligando o pool de threads...");
poolDeThreads.shutdown();
}
}
}
}