Netty 学习笔记二 ByteToMessageDecoder使用
来源:互联网 发布:2016年cpi年度数据 编辑:程序博客网 时间:2024/05/20 09:05
服务端使用上节的 Netty Time服务端
对客户端进行改写:
package io.netty.example.time;import java.util.List;import io.netty.buffer.ByteBuf;import io.netty.channel.ChannelHandlerContext;import io.netty.handler.codec.ByteToMessageDecoder;public class TimeDecoder extends ByteToMessageDecoder { // (1) @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { // (2) if (in.readableBytes()<4) { return; // (3) } out.add(in.readBytes(4)); // (4) }}
package io.netty.example.time;import io.netty.buffer.ByteBuf;import io.netty.channel.ChannelHandlerAdapter;import io.netty.channel.ChannelHandlerContext;import java.util.Date;public class TimeClientHandler extends ChannelHandlerAdapter { private ByteBuf buf; @Override public void handlerAdded(ChannelHandlerContext ctx) { buf = ctx.alloc().buffer(4); // (1) } @Override public void handlerRemoved(ChannelHandlerContext ctx) { buf.release(); // (1) buf = null; } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { ByteBuf m = (ByteBuf) msg; buf.writeBytes(m); // (2) m.release(); if (buf.readableBytes() >= 4) { // (3) long currentTimeMillis = (buf.readInt() - 2208988800L) * 1000L; System.out.println(new Date(currentTimeMillis)); ctx.close(); } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { cause.printStackTrace(); ctx.close(); }}
package io.netty.example.time;import io.netty.bootstrap.Bootstrap;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelOption;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.SocketChannel;import io.netty.channel.socket.nio.NioSocketChannel;public class TimeClient { public static void main(String[] args) throws Exception { String host = args[0]; int port = Integer.parseInt(args[1]); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); // (1) b.group(workerGroup); // (2) b.channel(NioSocketChannel.class); // (3) b.option(ChannelOption.SO_KEEPALIVE, true); // (4) b.handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new TimeDecoder(), new TimeClientHandler()); } }); // Start the client. ChannelFuture f = b.connect(host, port).sync(); // (5) // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); } }}
程序启动后,输出结果:
也可以把TimeDecoder.java从ReplayingDecoder继承:
package io.netty.example.time;import java.util.List;import io.netty.buffer.ByteBuf;import io.netty.channel.ChannelHandlerContext;import io.netty.handler.codec.ReplayingDecoder;public class TimeDecoder extends ReplayingDecoder<Void> { @Override protected void decode( ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { out.add(in.readBytes(4)); }}
运行也可以输出结果。
0 0
- Netty 学习笔记二 ByteToMessageDecoder使用
- Java Netty 学习笔记(二)使用Netty编程
- java netty之ByteToMessageDecoder
- java netty之ByteToMessageDecoder
- Netty 之 浅析ByteToMessageDecoder
- Netty 源码分析之ByteToMessageDecoder
- Netty 学习笔记之二 Netty 核心概念
- Netty学习笔记(二) Channel和ChannelFuture
- netty学习笔记二 内存管理
- 【Netty】netty学习笔记一
- Netty学习之二
- netty 学习记录二
- 一起学Netty(八)之 浅析ByteToMessageDecoder
- 一起学Netty(八)之 浅析ByteToMessageDecoder
- Netty中ByteToMessageDecoder中的decode方法执行多次的问题
- Netty源码学习笔记
- Netty学习笔记一
- Netty 学习笔记
- Windows XP中如何在注册表下查看隐藏用户?
- LOCAL_MODULE_TAGS 选项说明
- Android广播的两种注册使用方式。
- java float double为什么精度会丢失
- Android屏幕适配全攻略(最权威的官方适配指导)
- Netty 学习笔记二 ByteToMessageDecoder使用
- 如何在Myeclipse中把项目的J2ee版本升级
- getopt、getopt_long命令参数
- PLSQL Developer win7 64位 安装方法
- leetcode house Rubber II
- windows hosts
- html5中页面拨打电话的方式
- SAP 发送邮件例子(内部)
- Linux下用Eclipse开发调试Hadoop程序后打jar包后,到Hadoop集群上运行