Reputation: 58
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.
Upvotes: 4
Views: 4891
Reputation: 18865
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
Upvotes: 1