q502691450 2014-05-26 05:29 采纳率: 0%
浏览 7134

netty长连接服务器断开后,客户端如何重新连接

private final static Logger logger = LoggerFactory.getLogger(ResourceStatusReceiver.class);

private static Bootstrap b = new Bootstrap(); 


public static void start() throws Exception { 


    EventLoopGroup workerGroup = new NioEventLoopGroup(); 


    try { 

// b = new Bootstrap(); // (1)
b.group(workerGroup); // (2)
b.channel(NioSocketChannel.class); // (3)
b.option(ChannelOption.SO_KEEPALIVE, true); // (4)
b.option(ChannelOption.ALLOW_HALF_CLOSURE,true);

        b.handler(new ChannelInitializer<SocketChannel>() { 


            @Override 
            protected void initChannel(SocketChannel ch) throws Exception { 


                ChannelPipeline pipeline = ch.pipeline(); 


                // 以("\n")为结尾分割的 解码器 
                pipeline.addLast("framer", new DelimiterBasedFrameDecoder(Integer.MAX_VALUE, Delimiters.lineDelimiter())); 


                // 字符串解码 和 编码 
                pipeline.addLast("decoder", new StringDecoder()); 
                pipeline.addLast("encoder", new StringEncoder()); 


                // 自己的逻辑Handler 
                pipeline.addLast("handler", new SimpleChannelInboundHandler() { 


                    @Override 
                    protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object o) throws Exception { 


                        logger.debug("接收到的状态内容:" + o.toString()); 



                    } 

                    @Override 
                    public void channelRegistered(ChannelHandlerContext ctx) throws Exception { 


                        logger.debug("连接成功"); 
                    } 


                    @Override 
                    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { 



                    } 


                    @Override 
                    public void channelActive(ChannelHandlerContext ctx) throws Exception { 


                        logger.debug("======channelActive========"); 
                    } 


                    @Override 
                    public void channelInactive(ChannelHandlerContext ctx) throws Exception { 


                        logger.debug("======channelInactive========"); 
                    } 


                    @Override 
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { 
                        logger.debug("======exceptionCaught========"+cause.getMessage());                         

                    } 
                }); 

            } 
        }); 
        // Start the client. 
        ChannelFuture f = b.connect("127.0.0.1",8060).sync(); // (5) 
        // Wait until the connection is closed. 
        f.channel().closeFuture().sync(); 
    }finally{ 
            workerGroup.shutdownGracefully(); 
    } 
} 
  • 写回答

1条回答

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-10-27 15:53
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启