Netty 使用 LocalChannel/LocalServerChannel 报了一个连接异常?

JMCui
  • 1
新手上路,请多包涵

在使用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 上抛出问题,希望可以得到解答...

评论
阅读 2.3k
2 个回答
小胖子
  • 2
新手上路,请多包涵

检查仔细点,new 的 2个group都写错了,是 DefaultEventLoopGroup

你是开的两个main方法执行的吧,不在同一个虚拟机,连接不到,应该用一个main执行。

撰写回答

登录后参与交流、获取后续更新提醒

宣传栏