java - Netty 连接到 unix 域套接字失败

标签 java netty unix-socket

我正在编写一个小型Java程序,它使用Netty连接到unix域套接字以检索一些信息。我正在使用 Netty 4.0.32.Final 并使用 native epoll 包。这是我编写的引导代码:

final Bootstrap bootstrap = new Bootstrap();
bootstrap
    .group(new EpollEventLoopGroup())
    .channel(EpollDomainSocketChannel.class)
    .handler(
        new ChannelInitializer<DomainSocketChannel>() {
            @Override
            protected void initChannel(
                final DomainSocketChannel channel) throws Exception {
                channel.pipeline().addLast(
                    new ChannelInboundHandlerAdapter() {
                        @Override
                        public void channelRead(
                            final ChannelHandlerContext ctx,
                            final Object msg) throws Exception {
                            final ByteBuf buff = (ByteBuf) msg;
                            try {
                                buff.readBytes(
                                    DomainSocket.this.out,
                                    buff.readableBytes()
                                );
                            } finally {
                                buff.release();
                            }
                        }
                        @Override
                        public void exceptionCaught(
                            final ChannelHandlerContext ctx,
                            final Throwable cause) throws Exception {
                            Logger.error(
                                "Error occur when reading from Unix domain socket: %s",
                                cause.getMessage()
                            );
                            ctx.close();
                        }
                    }
                );
            }
        }
    );

对我来说看起来不错,但是当我运行时

bootstrap.connect(new DomainSocketAddress("/tmp/test.sock"));

它总是提示以下错误:

java.net.ConnectException: connect() failed: Connection refused: /tmp/test.sock
  at io.netty.channel.epoll.Native.newConnectException(Native.java:504)
  at io.netty.channel.epoll.Native.connect(Native.java:481)
  at io.netty.channel.epoll.AbstractEpollStreamChannel.doConnect(AbstractEpollStreamChannel.java:567)
  at io.netty.channel.epoll.EpollDomainSocketChannel.doConnect(EpollDomainSocketChannel.java:81)
  at io.netty.channel.epoll.AbstractEpollStreamChannel$EpollStreamUnsafe.connect(AbstractEpollStreamChannel.java:627)
  at io.netty.channel.DefaultChannelPipeline$HeadContext.connect(DefaultChannelPipeline.java:1097)

请问引导设置有什么问题吗?谢谢。

更新 我编写了一个简单的服务器来在单元测试中测试上面的代码。以下是服务器代码:

final ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap
        .group(new EpollEventLoopGroup(), new EpollEventLoopGroup())
        .channel(EpollServerDomainSocketChannel.class)
        .childHandler(
            new ChannelInitializer<ServerDomainSocketChannel>() {
                @Override
                protected void initChannel(
                    final ServerDomainSocketChannel channel)
                    throws Exception {
                    channel.pipeline().addLast(
                        new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelActive(
                                final ChannelHandlerContext ctx)
                                throws Exception {
                                final ByteBuf buff = ctx.alloc().buffer();
                                buff.writeBytes("This is a test".getBytes());
                                ctx.writeAndFlush(buff)
                                    .addListener(
                                        ChannelFutureListener.CLOSE
                                    );
                            }
                        }
                    );
                }
            }
        );
final ChannelFuture future =
    bootstrap.bind(new DomainSocketAddress(input)).sync();
future.channel().closeFuture().sync();

我使用 ExecutorService 在单独的线程中启动了此服务器代码。谢谢。

最佳答案

看起来该套接字上没有任何内容正在监听。您是否运行了任何服务器来监听?你能检查一下吗:

netstat -na | grep /tmp/test.sock

关于java - Netty 连接到 unix 域套接字失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33296749/

相关文章:

java - 随机百分比分支的编码模式?

java - netty 文件传输客户端

Netty 4/5实际上没有检测到bytebuf的资源泄漏?

c - 如何使用 recv 接收长文本字符串

java - 如何确定对整数数组进行合并排序的可能运行时间?

java - 编写一个 Java 8 谓词来过滤不在范围内的值

java - 如何将 Theta 360 度照片发布到 Facebook

netty - 找不到 io.netty.resolver.dns.macos.MacOSDnsServerAddressStreamProvider

c - 发送在循环中仅工作一次

c++ - 使用 Unix 域套接字 (UDS) 替代 splice(2)