From bfc423932ef0b4b93ab5947794c6cfe1d7459b04 Mon Sep 17 00:00:00 2001 From: Shuiling <1668589925@qq.com> Date: Sat, 7 Sep 2024 13:40:30 -0700 Subject: [PATCH] =?UTF-8?q?feat(Communication):=20=E4=BD=BF=E7=94=A8try-wi?= =?UTF-8?q?th-resources=E8=87=AA=E5=8A=A8=E5=85=B3=E9=97=ADSocket?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cc/baka9/catseedlogin/bukkit/Communication.java | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/main/java/cc/baka9/catseedlogin/bukkit/Communication.java b/src/main/java/cc/baka9/catseedlogin/bukkit/Communication.java index 2d8c02f..0bd900d 100644 --- a/src/main/java/cc/baka9/catseedlogin/bukkit/Communication.java +++ b/src/main/java/cc/baka9/catseedlogin/bukkit/Communication.java @@ -112,20 +112,15 @@ private static void handleConnectRequest(Socket socket, String playerName) { CatScheduler.runTask(() -> { boolean result = LoginPlayerHelper.isLogin(playerName); CatSeedLogin.instance.runTaskAsync(() -> { - try { - socket.getOutputStream().write(result ? 1 : 0); - socket.getOutputStream().flush(); // 确保数据发送 + try (Socket autoCloseSocket = socket) { + autoCloseSocket.getOutputStream().write(result ? 1 : 0); + autoCloseSocket.getOutputStream().flush(); // 确保数据发送 } catch (IOException e) { CatSeedLogin.instance.getLogger().warning("发送连接结果时发生错误: " + e.getMessage()); - } finally { - try { - socket.close(); - } catch (IOException e) { - CatSeedLogin.instance.getLogger().warning("关闭Socket时发生错误: " + e.getMessage()); - } } }); }); } + }