From c2bb34fd7e18a49b53a75647460a1df7cb33433a Mon Sep 17 00:00:00 2001 From: gonggy <514592053@qq.com> Date: Sat, 9 Jul 2022 17:53:52 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E6=97=A5=E5=BF=97=E4=BC=98=E5=8C=96=202.?= =?UTF-8?q?=20=E6=96=B9=E6=B3=95javadoc=E4=BC=98=E5=8C=96=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0throws=203.=20NioServer=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E6=97=B6=E8=87=AA=E5=8A=A8=E5=85=B3=E9=97=AD?= =?UTF-8?q?=E8=B5=84=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/cn/hutool/socket/SocketUtil.java | 6 ++++-- .../src/main/java/cn/hutool/socket/nio/NioClient.java | 5 ++++- .../src/main/java/cn/hutool/socket/nio/NioServer.java | 3 ++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/hutool-socket/src/main/java/cn/hutool/socket/SocketUtil.java b/hutool-socket/src/main/java/cn/hutool/socket/SocketUtil.java index 9d10163eb..7497189c7 100644 --- a/hutool-socket/src/main/java/cn/hutool/socket/SocketUtil.java +++ b/hutool-socket/src/main/java/cn/hutool/socket/SocketUtil.java @@ -23,8 +23,9 @@ public class SocketUtil { * * @param channel {@link AsynchronousSocketChannel} * @return 远程端的地址信息,包括host和端口,null表示channel为null或者远程主机未连接 + * @throws IORuntimeException IO异常 */ - public static SocketAddress getRemoteAddress(AsynchronousSocketChannel channel) { + public static SocketAddress getRemoteAddress(AsynchronousSocketChannel channel) throws IORuntimeException { try { return (null == channel) ? null : channel.getRemoteAddress(); } catch (ClosedChannelException e) { @@ -41,8 +42,9 @@ public class SocketUtil { * * @param channel {@link AsynchronousSocketChannel} * @return 远程主机是否处于连接状态 + * @throws IORuntimeException IO异常 */ - public static boolean isConnected(AsynchronousSocketChannel channel) { + public static boolean isConnected(AsynchronousSocketChannel channel) throws IORuntimeException { return null != getRemoteAddress(channel); } diff --git a/hutool-socket/src/main/java/cn/hutool/socket/nio/NioClient.java b/hutool-socket/src/main/java/cn/hutool/socket/nio/NioClient.java index f56bd531d..b1d2d8f55 100644 --- a/hutool-socket/src/main/java/cn/hutool/socket/nio/NioClient.java +++ b/hutool-socket/src/main/java/cn/hutool/socket/nio/NioClient.java @@ -3,6 +3,7 @@ package cn.hutool.socket.nio; import cn.hutool.core.io.IORuntimeException; import cn.hutool.core.io.IoUtil; import cn.hutool.core.thread.ThreadUtil; +import cn.hutool.log.Log; import cn.hutool.socket.SocketRuntimeException; import java.io.Closeable; @@ -22,6 +23,8 @@ import java.util.Iterator; */ public class NioClient implements Closeable { + private static final Log log = Log.get(); + private Selector selector; private SocketChannel channel; private ChannelHandler handler; @@ -91,7 +94,7 @@ public class NioClient implements Closeable { try { doListen(); } catch (IOException e) { - e.printStackTrace(); + log.error("Listen failed", e); } }); } diff --git a/hutool-socket/src/main/java/cn/hutool/socket/nio/NioServer.java b/hutool-socket/src/main/java/cn/hutool/socket/nio/NioServer.java index 7af2ab12f..d3eaee9c7 100644 --- a/hutool-socket/src/main/java/cn/hutool/socket/nio/NioServer.java +++ b/hutool-socket/src/main/java/cn/hutool/socket/nio/NioServer.java @@ -58,6 +58,7 @@ public class NioServer implements Closeable { // 服务器套接字注册到Selector中 并指定Selector监控连接事件 this.serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); } catch (IOException e) { + close(); throw new IORuntimeException(e); } @@ -140,7 +141,7 @@ public class NioServer implements Closeable { handler.handle(socketChannel); } catch (Exception e){ IoUtil.close(socketChannel); - StaticLog.error(e); + log.error(e); } } }