This commit is contained in:
Looly
2022-07-16 22:46:31 +08:00
parent 8e7d09cfc0
commit 681535c575
30 changed files with 1296 additions and 45 deletions

View File

@@ -23,8 +23,9 @@ public class SocketUtil {
*
* @param channel {@link AsynchronousSocketChannel}
* @return 远程端的地址信息包括host和端口null表示channel为null或者远程主机未连接
* @throws IORuntimeException IO异常
*/
public static SocketAddress getRemoteAddress(final AsynchronousSocketChannel channel) {
public static SocketAddress getRemoteAddress(final AsynchronousSocketChannel channel) throws IORuntimeException {
try {
return (null == channel) ? null : channel.getRemoteAddress();
} catch (final ClosedChannelException e) {
@@ -41,8 +42,9 @@ public class SocketUtil {
*
* @param channel {@link AsynchronousSocketChannel}
* @return 远程主机是否处于连接状态
* @throws IORuntimeException IO异常
*/
public static boolean isConnected(final AsynchronousSocketChannel channel) {
public static boolean isConnected(final AsynchronousSocketChannel channel) throws IORuntimeException{
return null != getRemoteAddress(channel);
}

View File

@@ -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;
@@ -21,6 +22,7 @@ import java.util.Iterator;
* @since 4.4.5
*/
public class NioClient implements Closeable {
private static final Log log = Log.get();
private Selector selector;
private SocketChannel channel;
@@ -32,6 +34,7 @@ public class NioClient implements Closeable {
* @param host 服务器地址
* @param port 端口
*/
@SuppressWarnings("resource")
public NioClient(final String host, final int port) {
init(new InetSocketAddress(host, port));
}
@@ -41,6 +44,7 @@ public class NioClient implements Closeable {
*
* @param address 服务器地址
*/
@SuppressWarnings("resource")
public NioClient(final InetSocketAddress address) {
init(address);
}
@@ -91,7 +95,7 @@ public class NioClient implements Closeable {
try {
doListen();
} catch (final IOException e) {
e.printStackTrace();
log.error("Listen failed", e);
}
});
}

View File

@@ -3,7 +3,6 @@ package cn.hutool.socket.nio;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import cn.hutool.log.Log;
import cn.hutool.log.StaticLog;
import java.io.Closeable;
import java.io.IOException;
@@ -34,6 +33,7 @@ public class NioServer implements Closeable {
*
* @param port 端口
*/
@SuppressWarnings("resource")
public NioServer(final int port) {
init(new InetSocketAddress(port));
}
@@ -58,6 +58,7 @@ public class NioServer implements Closeable {
// 服务器套接字注册到Selector中 并指定Selector监控连接事件
this.serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
} catch (final IOException e) {
close();
throw new IORuntimeException(e);
}
@@ -140,7 +141,7 @@ public class NioServer implements Closeable {
handler.handle(socketChannel);
} catch (final Exception e){
IoUtil.close(socketChannel);
StaticLog.error(e);
log.error(e);
}
}
}