4

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.

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply. I did write a server to listen to the file above. I updated my question above with server codes, which is still using Netty. Thanks.
Looks correct at the first sight. I assume input is equal to "/tmp/test.sock" . Can you check the output of the netstat I wrote earlier?
Thanks Zbynek. I found out that it was a synchronisation problem making the client connect to the socket earlier than the server starts up. I used some latches to synchronise them and it works now. Thanks for your tips to verify the socket. Also, I need to change the ChannelInitializer to be with type of UnixChannel to make it works as it had ClassCastException upon client connection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.