Netty和JDK版本冲突

时间:2018-09-18 15:08:42

标签: java netty

我正在使用教程研究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();

1 个答案:

答案 0 :(得分:1)

我怀疑您在此处输入了错误的SocketChannel。必须为io.netty.channel.socket.SocketChannel,但您最有可能使用了java.nio. SocketChannel