我正在使用教程学习netty工具,但我遇到了一个问题,我认为它可能与Netty包和JDK的版本有关。现在,我的jdk是1.8,Netty版本是netty 4.0.0 Final.jar,这是我从netty官方网站下载的。
下面是错误代码,,我在错误行后面使用了注释。因为我不知道如何在代码段中突出显示,所以您可能需要仔细注意注释,幸运的是只有两行。
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();
发布于 2018-09-19 00:09:32
我怀疑你在这里输入了错误的SocketChannel
。这需要是io.netty.channel.socket.SocketChannel
,但您很可能使用了java.nio. SocketChannel
。
https://stackoverflow.com/questions/52389647
复制相似问题