【问题标题】:How to close the socket from client side?如何从客户端关闭套接字?
【发布时间】:2012-04-23 17:32:36
【问题描述】:

我有一个使用 netty 开发的服务器和客户端。在客户端使用同一通道写入几条消息后,我尝试关闭套接字(释放资源)。我已经按照指示@http://netty.io/docs/unstable/guide/html/#start.12

完成上述步骤后,客户端仍然没有关闭。

哪位netty专家可以在这里帮助我。

问候 T

【问题讨论】:

    标签: java sockets netty


    【解决方案1】:

    该代码似乎缺少实际关闭通道的调用。我相信你真的想在第 14 行之前打电话给Channel.close()

    【讨论】:

      【解决方案2】:

      我在我的代码中这样做:

      public class TimeClient {
        public static void main(String[] args) throws Exception {
              ...
            ChannelFactory factory = ...;
              ClientBootstrap bootstrap = ...;
            ...
              ChannelFuture future(29) = bootstrap.connect(...);
            future.awaitUninterruptibly();(30)
              if (!future.isSuccess()) {
                future.getCause().printStackTrace();(31)
              }
            ... YOUR BUSINESS LOGIC HERE
            future.getChannel().close().awaitUninterruptibly();(32)
              factory.releaseExternalResources();(33)
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-17
        • 2015-04-07
        • 2011-01-27
        • 1970-01-01
        相关资源
        最近更新 更多