java - 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();
解决方案
我怀疑你在SocketChannel
这里输入错误。这需要,io.netty.channel.socket.SocketChannel
但您最有可能使用java.nio. SocketChannel
.
推荐阅读
- shell - 尝试将字符串回显到文件时出错
- npm - 发生意外错误:“EPERM:Yarn 中不允许操作
- python - 获取访问图权重的关键错误,networkx
- java - 如何解决 Java 中未解决的编译问题?
- android - phonegap 的空白模板 inappbrowser 插件问题
- spartacus-storefront - 斯巴达克斯-寄售回滚
- google-cloud-data-fusion - 获取 Cloud Data Fusion 运行时信息
- python - 在 Python 中运行后台进程
- javascript - 为什么这段 JS 和 PHP 代码不起作用?(我正在尝试将值从 JS 发送到 PHP)
- swift - macOS 中的 SQLite3 swift 无法获得一致的查询结果