This commit is contained in:
Mongolila-098
2026-04-28 13:35:30 +02:00
parent c480c4a831
commit d1bbc9fb28
29 changed files with 53 additions and 7 deletions

View File

@@ -6,6 +6,7 @@ import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketTimeoutException;
import java.net.SocketException;
import java.util.Map;
import java.util.Arrays;
import java.util.LinkedHashMap;
@@ -115,21 +116,32 @@ public class DirectoryConnector {
DatagramPacket pktFromServer = new DatagramPacket(responseData, responseData.length);
int intentos = 0;
boolean recibido = false;
//Hay que configurar el timeout antes del bucle
try {
socket.setSoTimeout(TIMEOUT);
} catch (SocketException e) {
System.err.println("* Error configurando el timeout del socket UDP: " + e.getMessage());
return null;
}
while (intentos < MAX_NUMBER_OF_ATTEMPTS && !recibido) {
try {
socket.send(pktToServer);
socket.setSoTimeout(TIMEOUT);
socket.receive(pktFromServer);
recibido = true;
} catch (IOException e) {
//excepciones tratadas
} catch (SocketTimeoutException e){
intentos++;
if (intentos == MAX_NUMBER_OF_ATTEMPTS) {
System.err.println("DirectoryConnector.sendAndReceiveDatagrams: socket.receive()");
System.exit(-1);
}
System.err.println("* Timeout. Reintentando... (" + intentos + "/" + MAX_NUMBER_OF_ATTEMPTS + ")");
} catch (IOException e) {
System.err.println("* Error grave de I/O en UDP: " + e.getMessage());
System.exit(-1);
}
}
if (intentos == MAX_NUMBER_OF_ATTEMPTS) {
System.err.println("* El directorio no responde tras " + MAX_NUMBER_OF_ATTEMPTS + " intentos.");
System.exit(-1);
}
String servResp = new String(responseData, 0, pktFromServer.getLength());
response = servResp.getBytes();
System.out.println("Hemos recibido " + servResp);

Binary file not shown.

Binary file not shown.