Timeout zu DatagramSocket hinzufügen - receive ()
Ich muss ein Timeout von 10 Sekunden für diesen Teil des Codes erstellen
DatagramPacket getack = new DatagramPacket (incoming, incoming.length);
socket.receive (getack);
Ich muss es für eingehende Pakete für 10s auflisten, wenn es ein Paket vor 10s empfängt, würde es zu if-Anweisung springen, wenn es 10s erreicht, würde es zu else springen und das Paket erneut senden. Ist das möglich und wie könnte ich das tun? Ich bin ziemlich neu darin.
private static void sendDATA() {
outgoing = new byte[512]; // Empty array
try {
ByteBuffer sDATA = ByteBuffer.allocate(516);
// 512 - 2 byte opcode, 2 byte block #, 512 data
DatagramPacket data = new DatagramPacket(outgoing, outgoing.length, InetAddress.getByName(clientip), clientport);
InputStream fis = new FileInputStream(new File(FILE));
int a;
int block = 1;
while((a = fis.read(outgoing,0,512)) != -1)
{
data.setLength(a);
sDATA.put((byte)3);
sDATA.put((byte)block);
sDATA.put(outgoing);
socket.send(data);
while(true) {
DatagramPacket getack = new DatagramPacket(incoming, incoming.length);
socket.receive(getack);
if(incoming[0] == 3 && incoming[1] == block) {
break;
} else {
socket.send(data);
}
}
}
} catch (Exception e) {
}
}