netty中channelRead为什么没有执行?

寂寥121470 2018-10-19 11:06:28
服务器端:
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();

}
}
...全文
3080 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
akmxl 2019-09-29
  • 打赏
  • 举报
回复
又实测了下,又2个地方要改,InboundHadler1取消掉,它会对消息做修改,导致InboundHadler2不能识别;2、ClientIntHandler生成ByteBuf的方式有问题。

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 {

// 注册两个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();
        }

    }

@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());
        ByteBuf encoded = Unpooled.copiedBuffer(msg, Charset.forName("utf-8"));
//        ctx.write(msg.getBytes());
        ctx.write(encoded);
        ctx.flush();

    }
akmxl 2019-09-29
  • 打赏
  • 举报
回复
你可以监控下InboundHadler1,read之后,消息是否发生了变化,导致InboundHadler2不能接收

50,528

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧