简体   繁体   中英

Netty connect to unix domain socket failed

I am writing a small Java program that using Netty to connect to a unix domain socket to retrieve some information. I am using Netty 4.0.32.Final and using native epoll package. Here is the bootstrap code I wrote:

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();
                        }
                    }
                );
            }
        }
    );

It looks fine to me but when I run

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

It always complains with the following errors:

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)

May I know is there anything wrong with the bootstrap setup? Thanks.

Update I wrote a simple server to test the code above in a unit test. Here are the server codes:

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();

I started this server code in a separate thread using ExecutorService . Thanks.

It looks like nothing is listening on that socket. Did you run any server listening there? Can you check:

netstat -na | grep /tmp/test.sock

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM