mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
commit
93012d7728
@ -23,8 +23,9 @@ public class SocketUtil {
|
|||||||
*
|
*
|
||||||
* @param channel {@link AsynchronousSocketChannel}
|
* @param channel {@link AsynchronousSocketChannel}
|
||||||
* @return 远程端的地址信息,包括host和端口,null表示channel为null或者远程主机未连接
|
* @return 远程端的地址信息,包括host和端口,null表示channel为null或者远程主机未连接
|
||||||
|
* @throws IORuntimeException IO异常
|
||||||
*/
|
*/
|
||||||
public static SocketAddress getRemoteAddress(AsynchronousSocketChannel channel) {
|
public static SocketAddress getRemoteAddress(AsynchronousSocketChannel channel) throws IORuntimeException {
|
||||||
try {
|
try {
|
||||||
return (null == channel) ? null : channel.getRemoteAddress();
|
return (null == channel) ? null : channel.getRemoteAddress();
|
||||||
} catch (ClosedChannelException e) {
|
} catch (ClosedChannelException e) {
|
||||||
@ -41,8 +42,9 @@ public class SocketUtil {
|
|||||||
*
|
*
|
||||||
* @param channel {@link AsynchronousSocketChannel}
|
* @param channel {@link AsynchronousSocketChannel}
|
||||||
* @return 远程主机是否处于连接状态
|
* @return 远程主机是否处于连接状态
|
||||||
|
* @throws IORuntimeException IO异常
|
||||||
*/
|
*/
|
||||||
public static boolean isConnected(AsynchronousSocketChannel channel) {
|
public static boolean isConnected(AsynchronousSocketChannel channel) throws IORuntimeException {
|
||||||
return null != getRemoteAddress(channel);
|
return null != getRemoteAddress(channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ package cn.hutool.socket.nio;
|
|||||||
import cn.hutool.core.io.IORuntimeException;
|
import cn.hutool.core.io.IORuntimeException;
|
||||||
import cn.hutool.core.io.IoUtil;
|
import cn.hutool.core.io.IoUtil;
|
||||||
import cn.hutool.core.thread.ThreadUtil;
|
import cn.hutool.core.thread.ThreadUtil;
|
||||||
|
import cn.hutool.log.Log;
|
||||||
import cn.hutool.socket.SocketRuntimeException;
|
import cn.hutool.socket.SocketRuntimeException;
|
||||||
|
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
@ -22,6 +23,8 @@ import java.util.Iterator;
|
|||||||
*/
|
*/
|
||||||
public class NioClient implements Closeable {
|
public class NioClient implements Closeable {
|
||||||
|
|
||||||
|
private static final Log log = Log.get();
|
||||||
|
|
||||||
private Selector selector;
|
private Selector selector;
|
||||||
private SocketChannel channel;
|
private SocketChannel channel;
|
||||||
private ChannelHandler handler;
|
private ChannelHandler handler;
|
||||||
@ -91,7 +94,7 @@ public class NioClient implements Closeable {
|
|||||||
try {
|
try {
|
||||||
doListen();
|
doListen();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
log.error("Listen failed", e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -58,6 +58,7 @@ public class NioServer implements Closeable {
|
|||||||
// 服务器套接字注册到Selector中 并指定Selector监控连接事件
|
// 服务器套接字注册到Selector中 并指定Selector监控连接事件
|
||||||
this.serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
|
this.serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
close();
|
||||||
throw new IORuntimeException(e);
|
throw new IORuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,7 +141,7 @@ public class NioServer implements Closeable {
|
|||||||
handler.handle(socketChannel);
|
handler.handle(socketChannel);
|
||||||
} catch (Exception e){
|
} catch (Exception e){
|
||||||
IoUtil.close(socketChannel);
|
IoUtil.close(socketChannel);
|
||||||
StaticLog.error(e);
|
log.error(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user