netty中channelRead为什么没有执行?
服务器端:
public class HelloServer {
public void start(int port)throws Exception{
EventLoopGroup bossGroup=new NioEventLoopGroup();
EventLoopGroup workerGroup=new NioEventLoopGroup();
try {
ServerBootstrap b=new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
// TODO Auto-generated method stub
// 注册两个OutboundHandler,执行顺序为注册顺序的逆序,所以应该是OutboundHandler2 OutboundHandler1
ch.pipeline().addLast(new OutboundHandler1());
ch.pipeline().addLast(new OutboundHandler2());
// 注册两个InboundHandler,执行顺序为注册顺序,所以应该是InboundHandler1 InboundHandler2
ch.pipeline().addLast(new InboundHandler1());
ch.pipeline().addLast(new InboundHandler2());
}
}).option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f=b.bind(port).sync();
System.out.println("Server:start to connect....");
f.channel().closeFuture().sync();
}finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception{
HelloServer server=new HelloServer();
server.start(8000);
}
}
public class InboundHandler1 extends ChannelInboundHandlerAdapter{
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("InboundHandler1.channelActive");
ctx.fireChannelActive();
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("InboundHandler1.channelRead");
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("InboundHandler1.channelReadComplete");
ctx.flush();
}
}
public class InboundHandler2 extends ChannelInboundHandlerAdapter{
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("InboundHandler2.channelActive");
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("InboundHandler2.channelRead");
ByteBuf result=(ByteBuf)msg;
byte[] result1=new byte[result.readableBytes()];
result.readBytes(result1);
String resultStr=new String(result1);
System.out.println("Client said:"+resultStr);
result.release();
ctx.write(msg);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("InboundHandler2.channelReadComplete");
ctx.flush();
}
}
public class OutboundHandler1 extends ChannelOutboundHandlerAdapter{
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
System.out.println("OutboundHandler1.write");
String response="I am ok";
ByteBuf encoded=ctx.alloc().buffer(4*response.length());
encoded.writeBytes(response.getBytes());
ctx.write(encoded);
ctx.flush();
}
}
public class OutboundHandler2 extends ChannelOutboundHandlerAdapter{
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
System.out.println("OutboundHandler2.write");
}
}
客户端:
public class HelloClient {
public void connect(String host,int port)throws Exception{
EventLoopGroup workerGroup=new NioEventLoopGroup();
try {
Bootstrap b=new Bootstrap();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
// TODO Auto-generated method stub
ch.pipeline().addLast(new HelloClientIntHandler());
}
});
System.out.println("Client:start to connect...");
ChannelFuture f=b.connect(host,port).sync();
f.channel().closeFuture().sync();
}finally{
workerGroup.shutdownGracefully();
}
}
public static void main(String[] args)throws Exception {
HelloClient client=new HelloClient();
client.connect("127.0.0.1", 8000);
}
}
public class HelloClientIntHandler extends ChannelInboundHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("HelloClientIntHandler.channelRead");
ByteBuf result=(ByteBuf)msg;
byte[] result1=new byte[result.readableBytes()];
result.readBytes(result1);
result.release();
ctx.close();
System.out.println("Server said:"+new String(result1));
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("HelloClientIntHandler.channelActive");
String msg="Are you ok?";
ByteBuf encoded=ctx.alloc().buffer(4* msg.length());
ctx.write(msg.getBytes());
ctx.write(encoded);
ctx.flush();
}
}