Netty 关闭/解除绑定服务器通道

Posted

技术标签:

【中文标题】Netty 关闭/解除绑定服务器通道【英文标题】:Netty close/unbind server channel 【发布时间】:2015-09-11 19:25:38 【问题描述】:

我是 Netty 的新手。我正在使用 Netty 4.0.28。我指的是《Netty In Action》一书中提供的 EchoServer 示例。

除此之外,一切正常。客户端在激活的通道上向服务器发送消息,服务器打印接收到的消息并将相同的消息发送回客户端。稍后客户端通道关闭,但服务器仍将侦听新连接。

我也想关闭服务器通道并想将服务器绑定到一个新端口。

如何关闭服务器通道?

public class EchoServer 

    private final int port;

    public EchoServer(int port) 
        this.port = port;
    

    public void start() throws Exception 
        EventLoopGroup group = new NioEventLoopGroup();
        try 
            ServerBootstrap b = new ServerBootstrap();
            b.group(group).channel(NioserverSocketChannel.class)
                    .localAddress(new InetSocketAddress(port))
                    .childHandler(new ChannelInitializer<SocketChannel>() 

                        @Override
                        protected void initChannel(SocketChannel ch)
                                throws Exception 
                            ch.pipeline().addLast(new EchoServerHandler());

                        
                    );
            ChannelFuture f = b.bind().sync();
            System.out.println(EchoServer.class.getName()
                    + " started and listen on " + f.channel().localAddress());

            f.channel().closeFuture().sync();
         finally 
            group.shutdownGracefully();
        
    

    public static void main(String[] args) throws Exception 
        if (args.length != 1) 
            System.err.println("Usage: " + EchoServer.class.getSimpleName()
                    + " <port>");
            return;
        
        int port = Integer.parseInt(args[0]);
        new EchoServer(port).start();
    



@Sharable
public class EchoServerHandler extends ChannelInboundHandlerAdapter 

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) 
        System.out.println("Server received: " + msg);
        ctx.write(msg);
    

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) 
        ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(
                ChannelFutureListener.CLOSE);
    

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) 
        cause.printStackTrace();
        ctx.close();
    


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 Exception 
        EventLoopGroup group = new NioEventLoopGroup();
        try 
            Bootstrap b = new Bootstrap();
            b.group(group).channel(NioSocketChannel.class)
                    .remoteAddress(new InetSocketAddress(host, port))
                    .handler(new ChannelInitializer<SocketChannel>() 

                        @Override
                        protected void initChannel(SocketChannel ch)
                                throws Exception 
                            ch.pipeline().addLast(new EchoClientHandler());

                        
                    );
            ChannelFuture f = b.connect().sync();
            f.channel().closeFuture().sync();
         finally 
            group.shutdownGracefully();
        
    

    public static void main(String[] args) throws Exception 
        if (args.length != 2) 
            System.err.println("Usage: " + EchoClient.class.getSimpleName()
                    + " <host> <port>");
            return;
        

        final String host = args[0];
        final int port = Integer.parseInt(args[1]);

        new EchoClient(host, port).start();;
    


@Sharable
public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> 

    @Override
    public void channelActive(ChannelHandlerContext ctx) 
        ctx.writeAndFlush(Unpooled.copiedBuffer("Netty Rocks!", CharsetUtil.UTF_8));
    

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg)
            throws Exception 
        System.out.println("Client received: "+ByteBufUtil.hexDump(msg.readBytes(msg.readableBytes())));
    

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) 
        cause.printStackTrace();
        ctx.close();
    

【问题讨论】:

【参考方案1】:

您必须区分“子”通道(您可以从处理程序中的 ctx.channel() 获取,附加到一个唯一的客户端连接,由 ctx 隐式引用)和“父”通道(您可以来自ctx.channel().parent())。

关闭“孩子”不会从您的主服务器中终止f.channel(),因为这是“父母”(监听器)。所以你必须做类似的事情

ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(
            ChannelFutureListener.CLOSE);
ctx.closeFuture().addListener(new ChannelFutureListener() 
        @Override
        public void operationComplete(ChannelFuture future) 
                ctx.channel().parent().close();
        
);

【讨论】:

以上是关于Netty 关闭/解除绑定服务器通道的主要内容,如果未能解决你的问题,请参考以下文章

当客户端被物理杀死时,总是无法从服务器端关闭通道(netty)

信用卡微信公众号或服务号解除绑定-实录

sh 从OD服务器解除绑定macOS客户端

在 Android 中解除绑定后,如何让服务与听众保持活力?

大漠插件解除窗口绑定命令

Netty进阶:手把手教你如何编写一个NIO服务端