Fix server throwing an ArrayOutOfBoundsException when someone tries to leave

This commit is contained in:
pixtaded 2025-01-12 15:06:43 +03:00
parent a7ad8770c9
commit c2e8927c42

View File

@ -30,10 +30,13 @@ public class ServerThread implements Runnable {
public void run() { public void run() {
try { try {
byte[] PID = input.readNBytes(1); byte[] PID = input.readNBytes(1);
if (PID.length == 0) {
socket.close();
return;
}
switch (PID[0]) { switch (PID[0]) {
case MESSAGE -> { case MESSAGE -> {
server.getDb().logMessage(new Date(), socket.getInetAddress().getHostAddress(), new String(input.readNBytes(4096), StandardCharsets.UTF_8).trim()); server.getDb().logMessage(new Date(), socket.getInetAddress().getHostAddress(), new String(input.readNBytes(4096), StandardCharsets.UTF_8).trim());
socket.close();
} case LOGS -> { } case LOGS -> {
respond(server.getDb().getLogs()); respond(server.getDb().getLogs());
} case LOGS_SIZE -> { } case LOGS_SIZE -> {
@ -42,6 +45,7 @@ public class ServerThread implements Runnable {
System.out.println("PID not implemented: " + PID[0]); System.out.println("PID not implemented: " + PID[0]);
} }
} }
socket.close();
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -50,7 +54,6 @@ public class ServerThread implements Runnable {
private void respond(byte[] data) throws IOException { private void respond(byte[] data) throws IOException {
socket.getOutputStream().write(data); socket.getOutputStream().write(data);
socket.getOutputStream().flush(); socket.getOutputStream().flush();
socket.close();
} }
private void respond(String utf8) throws IOException { private void respond(String utf8) throws IOException {