Netty 使用 LocalChannel/LocalServerChannel 报了一个连接异常?
在使用Netty jvm 通信的时候,试着使用 LocalServerChannel 和 LocalChannel,报了一个连接异常,代码如下:
服务端代码:
public void server() throws InterruptedException {
final EchoServerHandler serverHandler = new EchoServerHandler();
EventLoopGroup group = new DefaultEventLoop();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(group, group)
.channel(LocalServerChannel.class)
.childHandler(new ChannelInitializer<LocalChannel>() {
@Override
protected void initChannel(LocalChannel ch) {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(serverHandler);
}
});
ChannelFuture channelFuture = bootstrap.bind(new LocalAddress("foo")).sync();
System.out.println(EchoServer.class.getName() + "--started and listening for connections on--" + channelFuture.channel().localAddress());
channelFuture.channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}
客户端代码:
public void client() throws InterruptedException {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group)
.channel(LocalChannel.class)
.handler(new ChannelInitializer<LocalChannel>() {
@Override
protected void initChannel(LocalChannel ch) throws Exception {
ch.pipeline().addLast(new EchoClientHandler());
}
});
ChannelFuture channelFuture = bootstrap.connect(new LocalAddress("foo")).sync();
channelFuture.channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}
报错信息:
Exception in thread "main" io.netty.channel.ChannelException: connection refused
at io.netty.channel.local.LocalChannel$LocalUnsafe.connect(LocalChannel.java:375)
at io.netty.channel.DefaultChannelPipeline$HeadContext.connect(DefaultChannelPipeline.java:1245)
at io.netty.channel.ChannelHandlerInvokerUtil.invokeConnectNow(ChannelHandlerInvokerUtil.java:118)
at io.netty.channel.DefaultChannelHandlerInvoker.invokeConnect(DefaultChannelHandlerInvoker.java:238)
at io.netty.channel.PausableChannelEventExecutor.invokeConnect(PausableChannelEventExecutor.java:107)
at io.netty.channel.AbstractChannelHandlerContext.connect(AbstractChannelHandlerContext.java:493)
at io.netty.channel.AbstractChannelHandlerContext.connect(AbstractChannelHandlerContext.java:487)
at io.netty.channel.DefaultChannelPipeline.connect(DefaultChannelPipeline.java:1018)
at io.netty.channel.AbstractChannel.connect(AbstractChannel.java:241)
at io.netty.bootstrap.Bootstrap$3.run(Bootstrap.java:230)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:328)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)
at io.netty.util.internal.chmv8.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1412)
at io.netty.util.internal.chmv8.ForkJoinTask.doExec(ForkJoinTask.java:280)
at io.netty.util.internal.chmv8.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:877)
at io.netty.util.internal.chmv8.ForkJoinPool.scan(ForkJoinPool.java:1706)
at io.netty.util.internal.chmv8.ForkJoinPool.runWorker(ForkJoinPool.java:1661)
at io.netty.util.internal.chmv8.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:126)
报了这个异常后,我去搜了Google、查了Github 相关的Demo。都没有解决问题,几近崩溃中...现在在segmentfault 和 stackoverflow 上抛出问题,希望可以得到解答...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查仔细点,new 的 2个group都写错了,是 DefaultEventLoopGroup
你是开的两个main方法执行的吧,不在同一个虚拟机,连接不到,应该用一个main执行。