-
Notifications
You must be signed in to change notification settings - Fork 0
/
UDPServer.java
121 lines (105 loc) · 4 KB
/
UDPServer.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
public class UDPServer {
private DatagramSocket socket;
private boolean running;
private byte[] buf = new byte[1024]; // Buffer para recibir datos
private static List<ClientInfo> clients = new ArrayList<>(); // Lista para almacenar información de los clientes
public UDPServer(int port) throws SocketException {
this.socket = new DatagramSocket(port);
// Inicializa la lista de clientes
System.out.println("Servidor UDP iniciado en el puerto " + port);
}
public void start() {
running = true;
while (running) {
try {
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
handlePacket(packet);
} catch (IOException e) {
System.out.println("Error al recibir el paquete: " + e.getMessage());
}
}
socket.close();
}
private void handlePacket(DatagramPacket packet) {
String message = new String(packet.getData(), 0, packet.getLength());
InetAddress address = packet.getAddress();
int port = packet.getPort();
if (message.equals("GET_CLIENT_COUNT")) {
String response = ""+clients.size();
sendMessage(response, packet.getAddress(), packet.getPort());
}
if ("Estas?".equals(message)) {
sendMessage("si", packet.getAddress(), packet.getPort());
}
if (message.startsWith("LOGIN ")) {
String clientName = message.substring(6).trim();
ClientInfo newClient = new ClientInfo(address, port, clientName);
clients.add(newClient);
sendMessage("Hola " + clientName + ", bienvenido al chat.", address, port);
broadcastMessage(clientName + " se ha unido al chat.", newClient);
} else if (message.equals("adeu")) {
ClientInfo leavingClient = findClientByAddressAndPort(address, port);
if (leavingClient != null) {
clients.remove(leavingClient);
System.out.println(leavingClient.name + " ha dejado el chat.");
broadcastMessage(leavingClient.name + " ha dejado el chat.", leavingClient);
}
} else {
ClientInfo sender = findClientByAddressAndPort(address, port);
if (sender != null) {
System.out.println(message);
broadcastMessage(message, sender);
}
}
}
private ClientInfo findClientByAddressAndPort(InetAddress address, int port) {
for (ClientInfo client : clients) {
if (client.address.equals(address) && client.port == port) {
return client;
}
}
return null;
}
private void sendMessage(String message, InetAddress address, int port) {
try {
byte[] data = message.getBytes();
DatagramPacket packet = new DatagramPacket(data, data.length, address, port);
socket.send(packet);
} catch (IOException e) {
System.out.println("Error al enviar el mensaje: " + e.getMessage());
}
}
private void broadcastMessage(String message, ClientInfo sender) {
for (ClientInfo client : clients) {
if (!client.equals(sender)) {
sendMessage(message, client.address, client.port);
}
}
}
public static List<ClientInfo> getClients(){
return clients;
}
// private boolean verifyCapacity(){
// return true;
// if(clients.isEmpty() || ){}
// return false;
// }
}
class ClientInfo {
InetAddress address;
int port;
String name;
public ClientInfo(InetAddress addr, int port, String name) {
this.address = addr;
this.port = port;
this.name = name;
}
}