- 原理
按照开发者指定的固定长度对消息进行解码
- 案例
1.FixedLengthServer
/**
* @author pdc
*/
public class FixedLengthServer {
public static void main(String[] args) throws Exception {
int port = 8080;
new FixedLengthServer().bind(port);
}
public void bind(int port) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
//创建服务端辅助启动类ServerBootstrap对象
ServerBootstrap b = new ServerBootstrap();
//设置NIO线程组
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
//设置TCP参数,连接请求的最大队列长度
.option(ChannelOption.SO_BACKLOG, 1024)
//设置I/O事件处理类,用来处理消息的编解码以及我们的业务逻辑
.childHandler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
//设置FixedLengthFrameDecoder处理器,13为所需要传输的字符串,"你好,Netty!"的字节数组长度
ch.pipeline().addLast(new FixedLengthFrameDecoder(13));
//设置StringDecoder处理器
ch.pipeline().addLast(new Stri