代码片段。
尝试向与服务器建立的连接写一个问候。我假设一旦建立连接就会调用 channelActive 并使用上下文来编写响应。但是客户端什么也没收到。
public class EchoServerHandler extends ChannelInboundHandlerAdapter {
....
//edit checking status of future
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
// ChannelFuture f = ctx.write("hello"); //EDIT2 : cant use String use ByteBuf
ByteBuf msg = Unpooled.copiedBuffer("hello", CharsetUtil.UTF_8);
ctx.write(msg);
if (f.isSuccess()) {
System.out.println("success");
}
else {
System.out.println("failed");
f.cause().printStackTrace(); // EDIT2: Helpful in determining cause of failure
}
if (f.isDone()) {
System.out.println("Done");
}else
{
System.out.println("Not Done");
}
System.out.println("channelActive");
} .....
}