有一个回显服务器和一个回显客户端。当我使用kill -9 <client_pid>
杀死客户端进程时,将调用服务器上的channelInactive
。
客户端被SIGKILL
杀死,因此应该没有时间主动关闭此套接字。结果,不应触发服务器上的channelInactive
,这确实发生了。
当客户端被channelInactive
杀死时,为什么在服务器上调用SIGKILL
?
EchoServer类
public class EchoServer {
private final int port;
public EchoServer(int port) {
this.port = port;
}
public static void main(String[] args) throws InterruptedException {
int port = 9001;
new EchoServer(port).start();
}
public void start() throws InterruptedException {
EventLoopGroup group = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap
.group(group)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline()
.addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println("channelInactive()");
}
});
}
});
ChannelFuture future = bootstrap.bind(new InetSocketAddress(port)).sync();
future.channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}
}
EchoClient类
public class EchoClient {
private final String host;
private final int port;
public EchoClient(String host, int port) {
this.host = host;
this.port = port;
}
public void start() throws InterruptedException {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap
.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
}
});
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)).sync();
future.channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}
public static void main(String[] args) throws InterruptedException {
new EchoClient("localhost", 9001).start();
}
}
答案 0 :(得分:1)
客户端被SIGKILL杀死,因此应该没有时间主动关闭此套接字。
客户端应用程序未主动关闭套接字。如果不再有本地应用程序打开,则客户端OS内核会隐式关闭套接字。