日志行"测试"永远不会打印。谁能明白为什么? "连接"是要打印的最后一行。
这是在Android 8(26)和Netty 4.1.18.Final
上完成的我有这段代码:
Bootstrap b = new Bootstrap();
group = new NioEventLoopGroup();
Log.d(RegisterAttemptTcp.class.getName(), "connecting");
InetSocketAddress ria = new InetSocketAddress(toHostname, portDestination);
b.group(group).channel(NioSocketChannel.class).option(ChannelOption.IP_TOS, 24)
.remoteAddress(ria).handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new SimpleHandlerTest());
}
});
b.localAddress(portAttempt);
ChannelFuture future = b.connect().sync();
future.channel().closeFuture().await(8_000);
和
public class SimpleHandlerTest extends SimpleChannelInboundHandler<ByteBuf> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
Log.d(SimpleHandlerTest.class.getName(), "Testing");
}
}
答案 0 :(得分:0)
似乎您的连接尝试未完成(无论出于何种原因)。默认情况下,netty应在10秒后以超时异常失败连接操作的Future
。