Netty(三)之数据之粘包拆包

简介: Netty(三)之数据之粘包拆包

前提


Netty(一)之helloworld


数据的粘包


在上面的的例子基础之上的TimeClient上修改


我们的本意是发送三条您好


 //发送数据
            f.channel().writeAndFlush(Unpooled.copiedBuffer("您好".getBytes()));
            //Thread.sleep(1000);//防止TCP粘包
            f.channel().writeAndFlush(Unpooled.copiedBuffer("您好".getBytes()));
            //Thread.sleep(1000);
            f.channel().writeAndFlush(Unpooled.copiedBuffer("您好".getBytes()));
            //Thread.sleep(1000);


4.png


数据粘在一起了,就是数据的粘包


初步解决办法


   //发送数据
            f.channel().writeAndFlush(Unpooled.copiedBuffer("您好".getBytes()));
            Thread.sleep(1000);//防止TCP粘包
            f.channel().writeAndFlush(Unpooled.copiedBuffer("您好".getBytes()));
            Thread.sleep(1000);
            f.channel().writeAndFlush(Unpooled.copiedBuffer("您好".getBytes()));
            Thread.sleep(1000);


分隔符解码DelimiterBasedFrameDceoder


TimeServerHandler


package mydelimiterBasedFrameDecoder;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
/**
 * @author CBeann
 * @create 2019-08-27 18:31
 */
public class TimeServerHandler extends ChannelHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //服务器读客户端发送来的数据
        String body = (String) msg;//之所以能强制类型转换是因为添加了StringDecoder
        System.out.println("The TimeServer receive :" + body);
        //服务器向客户端回应请求
        String resp = "服务器端已经收到客户端数据$_";//$_结尾符号
        ByteBuf response = Unpooled.copiedBuffer(resp.getBytes());
        ctx.writeAndFlush(response);
    }
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
//
}


TimeServer


package mydelimiterBasedFrameDecoder;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
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.NioServerSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
/**
 * @author CBeann
 * @create 2019-08-27 18:22
 */
public class TimeServer {
    public static void main(String[] args) throws Exception {
        int port = 8080;
        //配置服务器端的NIO线程组
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            //设置特殊分隔符
                            ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
                            socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
                            //设置字符串编码器,使其在handler中的msg可以直接强转为String
                            socketChannel.pipeline().addLast(new StringDecoder());
                            //TimeClientHandler是自己定义的方法
                            socketChannel.pipeline().addLast(new TimeServerHandler());
                        }
                    });
            //绑定端口
            ChannelFuture f = b.bind(port).sync();
            //等待服务端监听端口关闭
            f.channel().closeFuture().sync();
        } catch (Exception e) {
        } finally {
            //优雅关闭,释放线程池资源
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}


TimeClientHandler


package mydelimiterBasedFrameDecoder;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.ReferenceCountUtil;
/**
 * @author CBeann
 * @create 2019-08-27 18:47
 */
public class TimeClientHandler extends ChannelHandlerAdapter {
    //客户端读取服务器发送的数据
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        try {
            String body = (String) msg;
            System.out.println("Now is:" + body);
        } catch (Exception e) {
        } finally {
            //标配
            ReferenceCountUtil.release(msg);
        }
    }
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}


TimeClient


package mydelimiterBasedFrameDecoder;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
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;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
/**
 * @author CBeann
 * @create 2019-08-27 18:43
 */
public class TimeClient {
    public static void main(String[] args) throws Exception {
        int port = 8080;
        String host = "127.0.0.1";
        //配置客户端NIO线程组
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY, true)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            //设置特殊分隔符
                            ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
                            socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
                            //设置字符串编码器,使其在handler中的msg可以直接强转为String
                            socketChannel.pipeline().addLast(new StringDecoder());
                            //TimeClientHandler是自己定义的方法
                            socketChannel.pipeline().addLast(new TimeClientHandler());
                        }
                    });
            //发起异步连接操作
            ChannelFuture f = b.connect(host, port).sync();
            //发送数据
            f.channel().writeAndFlush(Unpooled.copiedBuffer("您好$_".getBytes()));
            f.channel().writeAndFlush(Unpooled.copiedBuffer("您好$_".getBytes()));
            f.channel().writeAndFlush(Unpooled.copiedBuffer("您好$_".getBytes()));
            //等待客户端链路关闭
            f.channel().closeFuture().sync();
        } catch (Exception e) {
        } finally {
            //优雅关闭
            group.shutdownGracefully();
        }
    }
}


运行结果


5.png


6.png


定长解码器FixedLengthFrameDecoder


TimeServerHandler


读取数据,相应请求


package myfixedlengthframedecoder;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
/**
 * @author CBeann
 * @create 2019-08-27 18:31
 */
public class TimeServerHandler extends ChannelHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //服务器读客户端发送来的数据
        String body = (String) msg;
        System.out.println("The TimeServer receive :" + body);
        //服务器向客户端回应请求
        ByteBuf response = Unpooled.copiedBuffer("1234".getBytes());
        ctx.writeAndFlush(response);
    }
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
//
}


TimeServer


添加定长解码器


添加字符串解码器


package myfixedlengthframedecoder;
import io.netty.bootstrap.ServerBootstrap;
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.NioServerSocketChannel;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
/**
 * @author CBeann
 * @create 2019-08-27 18:22
 */
public class TimeServer {
    public static void main(String[] args) throws Exception {
        int port = 8080;
        //配置服务器端的NIO线程组
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            //添加固定长度解码器, 固定长度为5
                            socketChannel.pipeline().addLast(new FixedLengthFrameDecoder(5));
                            socketChannel.pipeline().addLast(new StringDecoder());
                            //TimeClientHandler是自己定义的方法
                            socketChannel.pipeline().addLast(new TimeServerHandler());
                        }
                    });
            //绑定端口
            ChannelFuture f = b.bind(port).sync();
            //等待服务端监听端口关闭
            f.channel().closeFuture().sync();
        } catch (Exception e) {
        } finally {
            //优雅关闭,释放线程池资源
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}


TimeClientHandler

接收服务器发来的数据

package myfixedlengthframedecoder;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.ReferenceCountUtil;
/**
 * @author CBeann
 * @create 2019-08-27 18:47
 */
public class TimeClientHandler extends ChannelHandlerAdapter {
    //客户端读取服务器发送的数据
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        try {
            String body = (String) msg;
            System.out.println("Now is:" + body);
        } catch (Exception e) {
        } finally {
            //标配
            ReferenceCountUtil.release(msg);
        }
    }
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}


TimeClient


添加定长解码器


添加字符串解码器


发送长度为10的数据(定长解码器设置长度为5)


package myfixedlengthframedecoder;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
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;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
/**
 * @author CBeann
 * @create 2019-08-27 18:43
 */
public class TimeClient {
    public static void main(String[] args) throws Exception {
        int port = 8080;
        String host = "127.0.0.1";
        //配置客户端NIO线程组
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY, true)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            socketChannel.pipeline().addLast(new FixedLengthFrameDecoder(5));
                            socketChannel.pipeline().addLast(new StringDecoder());
                            //TimeClientHandler是自己定义的方法
                            socketChannel.pipeline().addLast(new TimeClientHandler());
                        }
                    });
            //发起异步连接操作
            ChannelFuture f = b.connect(host, port).sync();
            //发送数据
            f.channel().writeAndFlush(Unpooled.copiedBuffer("0123456789".getBytes()));
            //等待客户端链路关闭
            f.channel().closeFuture().sync();
        } catch (Exception e) {
        } finally {
            //优雅关闭
            group.shutdownGracefully();
        }
    }
}


运行结果


客户端发送10个长度的字符串,因为设置了长度为5的定长解码器,所以服务器收到2条消息


7.png

目录
相关文章
|
编解码 网络协议 开发者
Netty运行原理问题之NettyTCP的粘包和拆包的问题如何解决
Netty运行原理问题之NettyTCP的粘包和拆包的问题如何解决
152 1
|
移动开发 网络协议 算法
(十)Netty进阶篇:漫谈网络粘包、半包问题、解码器与长连接、心跳机制实战
在前面关于《Netty入门篇》的文章中,咱们已经初步对Netty这个著名的网络框架有了认知,本章的目的则是承接上文,再对Netty中的一些进阶知识进行阐述,毕竟前面的内容中,仅阐述了一些Netty的核心组件,想要真正掌握Netty框架,对于它我们应该具备更为全面的认知。
934 2
|
网络协议
netty粘包问题分析
netty粘包问题分析
189 0
|
Java
Netty传输object并解决粘包拆包问题
Netty传输object并解决粘包拆包问题
205 0
|
Java
Netty中粘包拆包问题解决探讨
Netty中粘包拆包问题解决探讨
200 0
|
存储 缓存 NoSQL
跟着源码学IM(十一):一套基于Netty的分布式高可用IM详细设计与实现(有源码)
本文将要分享的是如何从零实现一套基于Netty框架的分布式高可用IM系统,它将支持长连接网关管理、单聊、群聊、聊天记录查询、离线消息存储、消息推送、心跳、分布式唯一ID、红包、消息同步等功能,并且还支持集群部署。
13930 1
|
9月前
|
算法 Java 容器
Netty源码—4.客户端接入流程
本文主要介绍了关于Netty客户端连接接入问题整理、Reactor线程模型和服务端启动流程、Netty新连接接入的整体处理逻辑、新连接接入之检测新连接、新连接接入之创建NioSocketChannel、新连接接入之绑定NioEventLoop线程、新连接接入之注册Selector和注册读事件、注册Reactor线程总结、新连接接入总结
|
9月前
|
安全 Java 调度
Netty源码—3.Reactor线程模型二
本文主要介绍了NioEventLoop的执行总体框架、Reactor线程执行一次事件轮询、Reactor线程处理产生IO事件的Channel、Reactor线程处理任务队列之添加任务、Reactor线程处理任务队列之执行任务、NioEventLoop总结。
|
9月前
|
安全 Java
Netty源码—2.Reactor线程模型一
本文主要介绍了关于NioEventLoop的问题整理、理解Reactor线程模型主要分三部分、NioEventLoop的创建和NioEventLoop的启动。
|
9月前
|
编解码 安全 Java
Netty源码—1.服务端启动流程
本文主要介绍了服务端启动整体流程及关键方法、服务端启动的核心步骤、创建服务端Channel的源码、初始化服务端Channel的源码、注册服务端Channel的源码、绑定服务端端口的源码、服务端启动流程源码总结。