【问题标题】:NettyIO disconnect Client from ServerNettyIO 断开客户端与服务器的连接
【发布时间】:2015-05-17 02:35:18
【问题描述】:

如何断开 netty 客户端与服务器的连接,使其在服务器端执行 handerRemoved 方法并完全停止运行?我尝试使用group.shutDownGraceFully(),但客户端仍然保持连接到服务器。有什么我缺少的方法吗?

我还注意到,当我尝试连接到服务器并且它无法访问(连接被拒绝)时,下次我连接到真实服务器时它会连接,但它不会发送或接收更多消息。

【问题讨论】:

    标签: java connection handler netty channel


    【解决方案1】:

    总的来说,您似乎对网络编程很陌生。

    我是 netty 的新手,所以请不要把我说的任何话都当作 100% 正确,尤其是没有接近 100% 的效率。

    所以网络编程的一个主要基本事实是客户端和服务器没有直接链接(显然)。为了在服务器上执行一个方法,您需要从客户端向服务器发送一条消息。例如:

    您在客户端拥有什么:

     //on shutdown
    {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    

    你想要什么:

    {
            yourchannelname.writeAndFlush("bye"+"\r\n")
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    

    当服务器收到再见命令时:

       // If user typed the 'bye' command, wait until the server closes
                // the connection.
                if ("bye".equals(line.toLowerCase())) {
                    ch.closeFuture().sync();
                    break;
                }
            }
            //this is for safety reasons, it is optional-ish
    
            // Wait until all messages are flushed before closing the channel.
            if (lastWriteFuture != null) {
                lastWriteFuture.sync();
            }
          //what you already have
        } finally {
            // The connection is closed automatically on shutdown.
            group.shutdownGracefully();
        }
    }
    

    希望这会有所帮助,我之前从未回答过有关堆栈溢出的问题,所以我希望我至少听起来我知道我在做什么:P

    【讨论】:

    • :很高兴我帮助你完成了一些事情
    猜你喜欢
    • 2013-03-31
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    相关资源
    最近更新 更多