【问题标题】:Netty connect to unix domain socket failedNetty 连接到 unix 域套接字失败
【发布时间】:2015-10-24 10:52:51
【问题描述】:

我正在编写一个小型 Java 程序,它使用 Netty 连接到 unix 域套接字以检索一些信息。我正在使用 Netty 4.0.32.Final 并使用本机 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 在单独的线程中启动了这个服务器代码。谢谢。

【问题讨论】:

    标签: java netty unix-socket


    【解决方案1】:

    该套接字上似乎没有任何东西在监听。你在那里运行任何服务器监听吗?你能检查一下吗:

    netstat -na | grep /tmp/test.sock
    

    【讨论】:

    • 感谢您的回复。我确实写了一个服务器来收听上面的文件。我用仍在使用 Netty 的服务器代码更新了上面的问题。谢谢。
    • 乍一看是正确的。我假设输入等于 "/tmp/test.sock" 。你能检查一下我之前写的netstat 的输出吗?
    • 谢谢 Zbynek。我发现这是一个同步问题,使客户端在服务器启动之前连接到套接字。我使用了一些锁存器来同步它们,它现在可以工作了。感谢您提供验证套接字的提示。另外,我需要将ChannelInitializer 更改为UnixChannel 类型,以使其在客户端连接时出现ClassCastException。
    猜你喜欢
    • 1970-01-01
    • 2020-02-15
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多