netty SimpleChannelInboundHandler类继承使用

继承一个SimpleChannelInboundHandler来实现我们的Client,我们需要重写其中的三个方法:

package NettyDemo.echo.handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.util.CharsetUtil;
@Sharable
public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {  
    /**   
    *此方法会在连接到服务器后被调用 
    * */  
    public void channelActive(ChannelHandlerContext ctx) {    
        ctx.write(Unpooled.copiedBuffer("Netty rocks!", CharsetUtil.UTF_8));  
    }  
    /**   
    *此方法会在接收到服务器数据后调用 
   * */  
   public void channelRead0(ChannelHandlerContext ctx, ByteBuf in) {    
       System.out.println("Client received: " + ByteBufUtil.hexDump(
       in.readBytes(in.readableBytes())));  
    }
   /**   
   *捕捉到异常 
   * */  
   public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {    
       cause.printStackTrace();    
       ctx.close();  
   }

}

    其中需要注意的是 channelRead0()方法,此方法接收到的可能是一些数据片段,比如服务器发送了5个字节数据,Client端不能保证一次全部收到,比如第一次收到3个字节,第二次收到2个字节。我们可能还会关心收到这些片段的顺序是否可发送顺序一致,这要看具体是什么协议,比如基于TCP协议的字节流是能保证顺序的。

    还有一点,在Client端我们的业务Handler继承的是SimpleChannelInboundHandler,而在服务器端继承的是ChannelInboundHandlerAdapter,那么这两个有什么区别呢?最主要的区别就是SimpleChannelInboundHandler在接收到数据后会自动release掉数据占用的Bytebuffer资源(自动调用Bytebuffer.release())。而为何服务器端不能用呢,因为我们想让服务器把客户端请求的数据发送回去,而服务器端有可能在channelRead方法返回前还没有写完数据,因此不能让它自动release。


转载于:https://my.oschina.net/u/1013711/blog/383006

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值