首页 > 解决方案 > Netty 和 JDK 版本冲突

问题描述

我正在使用教程学习netty工具,但我遇到了一个问题,我认为它可能与Netty jar包和JDK的版本有关。现在我的jdk是1.8,Netty版本是从netty官网下载的netty-all-4.0.0 Final.jar。

以下是错误代码,我在错误行后面使用了注释。因为我不知道如何在代码段中突出显示,你可能需要仔细注意注释,幸运的是只有两行。

    EventLoopGroup pGroup = new NioEventLoopGroup();    
    EventLoopGroup cGroup = new NioEventLoopGroup();    
    ServerBootstrap b = new ServerBootstrap();

    b.group(pGroup, cGroup)                             
    .channel(NioServerSocketChannel.class)              
    .option(ChannelOption.SO_BACKLOG, 1024)             
    .option(ChannelOption.SO_SNDBUF, 32*1024)           
    .option(ChannelOption.SO_RCVBUF, 32*1024)           
    .option(ChannelOption.SO_KEEPALIVE, true)           
    .childHandler(new ChannelInitializer<SocketChannel>() {        // there is an error, which indicates that the generic <SocketChannel> is not a valid substitute according the eclipse automatic prompt

        @Override
        protected void initChannel(SocketChannel sc) throws Exception {
            sc.pipeline().addLast(new ServerHandler());            // there are two errors about the pipeline method and ServerHander construtor
        }

    });

    ChannelFuture cf1 = b.bind().sync();                

    cf1.channel().closeFuture().sync();                 

    pGroup.shutdownGracefully();
    cGroup.shutdownGracefully();

标签: javanetty

解决方案


我怀疑你在SocketChannel这里输入错误。这需要,io.netty.channel.socket.SocketChannel但您最有可能使用java.nio. SocketChannel.


推荐阅读