From 6729de5894fbdf6320965ae3578de9d4e6d28e57 Mon Sep 17 00:00:00 2001 From: Dereku Date: Wed, 4 Sep 2024 10:21:01 +0700 Subject: [PATCH] Ignore EOFException too. try-catch + auto-closeable --- .../java/net/rubeta/legacyping/ServerThread.java | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/rubeta/legacyping/ServerThread.java b/src/main/java/net/rubeta/legacyping/ServerThread.java index 2a46093..bf2aae1 100644 --- a/src/main/java/net/rubeta/legacyping/ServerThread.java +++ b/src/main/java/net/rubeta/legacyping/ServerThread.java @@ -55,12 +55,11 @@ public class ServerThread extends Thread { public void run() { this.working.set(true); while (this.working.get() && !this.serverSocket.isClosed()) { - try { - final Socket socket = this.serverSocket.accept(); + try (final Socket socket = this.serverSocket.accept(); + final DataInputStream inputStream = new DataInputStream(socket.getInputStream()); + final DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream())) { socket.setTrafficClass(SocketOptions.TCP_NODELAY & SocketOptions.IP_TOS); -// System.out.println("[LegacyPing] Got new ping from " + socket.getRemoteSocketAddress()); - final DataInputStream inputStream = new DataInputStream(socket.getInputStream()); - final DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream()); + final int packetLength = inputStream.readByte() | inputStream.readByte() << 1; // idk why it doesn't a VarInt final int protocolVersion = this.readVarInt(inputStream); final String hostname = this.readUtf(inputStream); @@ -76,15 +75,10 @@ public class ServerThread extends Thread { outputStream.flush(); inputStream.readShort(); // legacy data - this.writeVarInt(outputStream, this.readVarInt(inputStream)); // Packet length (9) this.writeVarInt(outputStream, this.readVarInt(inputStream)); // Packet id (1) outputStream.writeLong(inputStream.readLong()); // timestamp - - inputStream.close(); - outputStream.close(); - socket.close(); - } catch (SocketTimeoutException ignore) { + } catch (SocketTimeoutException | EOFException ignore) { } catch (IOException ex) { System.out.println("Exception caught: " + ex); }