-
Notifications
You must be signed in to change notification settings - Fork 0
/
TCPServer.java
41 lines (30 loc) · 1.25 KB
/
TCPServer.java
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
import java.io.*;
import java.net.*;
import java.util.*;
public class TCPServer {
private static List<TCPClient> clients = new ArrayList<>();
private static ServerSocket serverSocket;
public static void startServer(ServerConfig config) throws IOException {
serverSocket = new ServerSocket(config.getPort());
System.out.println("Servidor TCP iniciado en el puerto " + config.getPort());
while (true) {
Socket clientSocket = serverSocket.accept();
if (clients.size() >= config.getMaxClients()) {
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
out.println("El servidor esta lleno. Por favor, intentalo mas tarde.");
clientSocket.close(); //se cierra la conexion.
} else {
TCPClient clientHandler = new TCPClient(clientSocket);
clients.add(clientHandler);
new Thread(clientHandler).start();
}
}
}
//getters y setters
public static List<TCPClient> getClients() {
return clients;
}
public static void setClients(List<TCPClient> clients) {
TCPServer.clients = clients;
}
}