概述:

Netty--数据通信和心跳检测

netty的ReadTimeOut实现方案3

服务端:

public class Server {

    public static void main(String[] args) throws Exception{
        
        EventLoopGroup pGroup = new NioEventLoopGroup();
        EventLoopGroup cGroup = new NioEventLoopGroup();
        
        ServerBootstrap b = new ServerBootstrap();
        b.group(pGroup, cGroup)
         .channel(NioServerSocketChannel.class)
         .option(ChannelOption.SO_BACKLOG, 1024)
         //设置日志
         .handler(new LoggingHandler(LogLevel.INFO))
         .childHandler(new ChannelInitializer<SocketChannel>() {
            protected void initChannel(SocketChannel sc) throws Exception {
                sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());
                sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());
                sc.pipeline().addLast(new ReadTimeoutHandler(5)); 
                sc.pipeline().addLast(new ServerHandler());
            }
        });
        
        ChannelFuture cf = b.bind(8765).sync();
        
        cf.channel().closeFuture().sync();
        pGroup.shutdownGracefully();
        cGroup.shutdownGracefully();
        
    }
}
View Code

相关文章:

  • 2021-10-21
  • 2021-07-21
  • 2021-06-26
  • 2022-01-11
  • 2021-05-28
  • 2021-11-05
  • 2021-08-26
  • 2022-12-23
猜你喜欢
  • 2021-07-26
  • 2021-09-06
  • 2021-05-25
  • 2022-02-22
  • 2022-01-23
  • 2021-06-05
相关资源
相似解决方案