Netty实战3——以回车换行结尾的消息如何处理半包问题

概述

TCP底层会发生粘包和拆包,这个是TCP的一个特性。为了减少网络数据传输的次数,TCP总是希望让网络数据到达一定量级的时候才将数据发送出去,而不是缓存区一有数据就马上发送数据。

TCP底层会根据缓冲区是否被填满了,来决定是否发送数据。但是从业务层面上看,这个是不合理的。因为一份业务数据可能很小,还不足以填满缓冲区,这样底层TCP就不会立刻把这份业务数据发送出去,而是等到好几份业务数据的大小填满缓冲区后才发送。这样子无论是服务端还是客户端,接收数据的时候可能会处理出错。

如果是以回车换行结尾的消息,那么组合使用

LineBasedFrameDecoder
StringDecoder

来处理半包问题。

服务端代码

package tcp.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class SocketServer {
    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup parentGroup = new NioEventLoopGroup();
        EventLoopGroup childGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(parentGroup, childGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new SocketServerInitializer());

            ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();
            channelFuture.channel().closeFuture().sync();
        }
        finally {
            parentGroup.shutdownGracefully();
            childGroup.shutdownGracefully();
        }
    }
}


package tcp.server;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;


public class SocketServerInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new SocketServerHandler());
    }
}


package tcp.server;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.Date;

public class SocketServerHandler extends ChannelInboundHandlerAdapter {
    private int counter;
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf buf = (ByteBuf)msg;
        byte[] req = new byte[buf.readableBytes()];
        //将buf中的数据读取到req中
        buf.readBytes(req);

        //打印客户端发送的数据
        String body = new String(req, "UTF-8").substring(0, req.length - 1);
        System.out.println("server receive order:"+body+";the counter is:"+ ++counter);

        String currentTime = "";
        if ("QUERY TIME ORDER".equalsIgnoreCase(body)) {
            currentTime = new Date(System.currentTimeMillis()).toString();
        }
        else {
            currentTime = "BAD ORDER";
        }

        String separator = "\n";
        currentTime = currentTime + separator;

        ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes());
        ctx.writeAndFlush(resp);
    }
}

客户端代码

package tcp.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

public class SocketClient {
    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup eventLoopGroup = new NioEventLoopGroup();

        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(eventLoopGroup)
                    .channel(NioSocketChannel.class)
                    .handler(new SocketClientInitializer());

            ChannelFuture channelFuture = bootstrap.connect("localhost", 8899).sync();
            channelFuture.channel().closeFuture().sync();
        }
        finally {
            eventLoopGroup.shutdownGracefully();
        }
    }
}

package tcp.client;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;

public class SocketClientInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new SocketClientHandler());
    }
}

package tcp.client;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class SocketClientHandler extends ChannelInboundHandlerAdapter {
    private int counter;
    private byte[] req;

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf buf = (ByteBuf)msg;
        byte[] req = new byte[buf.readableBytes()];

        buf.readBytes(req);
        String body = new String(req, "UTF-8");
        System.out.println("Now is : " + body + " ; the counter is :"+ ++counter);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ByteBuf message = null;
        for (int i = 0; i < 100; i++) {
            message = Unpooled.buffer(req.length);
            message.writeBytes(req);
            ctx.writeAndFlush(message);
        }
    }

    public SocketClientHandler() {
      req = ("QUERY TIME ORDER" + "\n").getBytes();
    }
}

当客户端与服务端建立完连接后,客户端中的SocketClientHandler类中的

channelActive

方法会被调用,往服务端发送100次请求,每个请求的内容都是

“QUERY TIME ORDER” + “\n”

当服务端接收到这些请求时,SocketServerHandler类中的

channelRead

方法会被调用,接到客户端发送的消息,如果每次收到的消息都是

QUERY TIME ORDER

那么服务端会往客户端输出当前时间。

当客户端接收到服务端的请求后,客户端类SocketClientHandler的

channelRead

方法会被调用,输出服务端返回的数据。

理论上,我们是希望客户端发送100次请求,服务端响应后,也发送100次响应。但实际上,客户端只发送了两次请求,同时服务端响应客户端的时候,只是发送了一次请求。

服务端真实打印的信息如下

server receive order:QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUE;the counter is:1
server receive order:Y TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER;the counter is:2

注意分析上面的输出,从

server receive order:QUERY TIME ORDER

到第一个

the counter is:1

中间有非常多的

QUERY TIME ORDER

说明客户端虽然发送了100次请求,但其实是累积了很多数据后,才真正的发送了一次数据。结果100次调用,最终只给服务端发送了两次请求。

既然服务端只是接收了2次请求,那么应该也给客户端发送两次请求,但是事实上只发送了一次,客户端的输出结果如下

Now is : BAD ORDER
BAD ORDER
 ; the counter is :1

有两个

BAD ORDER

说明服务端只给客户端发送了一次请求。

到此我们得出结论,在这个例子中客户端和服务端都发送了粘包

netty自带的解码器解决粘包和拆包问题

可以使用自带的

LineBasedFrameDecoder和StringDecoder

解码器来解决这个问题,我们稍微调整一下代码。

在SocketServerInitializer类中加入

pipeline.addLast(new LineBasedFrameDecoder(1024));
pipeline.addLast(new StringDecoder());

在SocketClientInitializer类中也加入

pipeline.addLast(new LineBasedFrameDecoder(1024));
pipeline.addLast(new StringDecoder());

将SocketServerHandler类channelRead方法中的

ByteBuf buf = (ByteBuf)msg;
byte[] req = new byte[buf.readableBytes()];
//将buf中的数据读取到req中
 buf.readBytes(req);

 //打印客户端发送的数据
 String body = new String(req, "UTF-8").substring(0, req.length - 1);

用下面一行代码替换即可

String body = (String)msg;

同样也将SocketClientHandler类中channelRead方法中的

 ByteBuf buf = (ByteBuf)msg;
 byte[] req = new byte[buf.readableBytes()];

 buf.readBytes(req);
 String body = new String(req, "UTF-8");

换成

String body = (String)msg;

接下来运行服务端和客户端,发现输出结果已经正常了。
服务端输出

server receive order:QUERY TIME ORDER;the counter is:1
server receive order:QUERY TIME ORDER;the counter is:2
server receive order:QUERY TIME ORDER;the counter is:3
server receive order:QUERY TIME ORDER;the counter is:4
server receive order:QUERY TIME ORDER;the counter is:5
server receive order:QUERY TIME ORDER;the counter is:6
server receive order:QUERY TIME ORDER;the counter is:7
server receive order:QUERY TIME ORDER;the counter is:8
server receive order:QUERY TIME ORDER;the counter is:9
server receive order:QUERY TIME ORDER;the counter is:10
server receive order:QUERY TIME ORDER;the counter is:11
server receive order:QUERY TIME ORDER;the counter is:12
server receive order:QUERY TIME ORDER;the counter is:13
server receive order:QUERY TIME ORDER;the counter is:14
server receive order:QUERY TIME ORDER;the counter is:15
server receive order:QUERY TIME ORDER;the counter is:16
server receive order:QUERY TIME ORDER;the counter is:17
server receive order:QUERY TIME ORDER;the counter is:18
server receive order:QUERY TIME ORDER;the counter is:19
server receive order:QUERY TIME ORDER;the counter is:20
server receive order:QUERY TIME ORDER;the counter is:21
server receive order:QUERY TIME ORDER;the counter is:22
server receive order:QUERY TIME ORDER;the counter is:23
server receive order:QUERY TIME ORDER;the counter is:24
server receive order:QUERY TIME ORDER;the counter is:25
server receive order:QUERY TIME ORDER;the counter is:26
server receive order:QUERY TIME ORDER;the counter is:27
server receive order:QUERY TIME ORDER;the counter is:28
server receive order:QUERY TIME ORDER;the counter is:29
server receive order:QUERY TIME ORDER;the counter is:30
server receive order:QUERY TIME ORDER;the counter is:31
server receive order:QUERY TIME ORDER;the counter is:32
server receive order:QUERY TIME ORDER;the counter is:33
server receive order:QUERY TIME ORDER;the counter is:34
server receive order:QUERY TIME ORDER;the counter is:35
server receive order:QUERY TIME ORDER;the counter is:36
server receive order:QUERY TIME ORDER;the counter is:37
server receive order:QUERY TIME ORDER;the counter is:38
server receive order:QUERY TIME ORDER;the counter is:39
server receive order:QUERY TIME ORDER;the counter is:40
server receive order:QUERY TIME ORDER;the counter is:41
server receive order:QUERY TIME ORDER;the counter is:42
server receive order:QUERY TIME ORDER;the counter is:43
server receive order:QUERY TIME ORDER;the counter is:44
server receive order:QUERY TIME ORDER;the counter is:45
server receive order:QUERY TIME ORDER;the counter is:46
server receive order:QUERY TIME ORDER;the counter is:47
server receive order:QUERY TIME ORDER;the counter is:48
server receive order:QUERY TIME ORDER;the counter is:49
server receive order:QUERY TIME ORDER;the counter is:50
server receive order:QUERY TIME ORDER;the counter is:51
server receive order:QUERY TIME ORDER;the counter is:52
server receive order:QUERY TIME ORDER;the counter is:53
server receive order:QUERY TIME ORDER;the counter is:54
server receive order:QUERY TIME ORDER;the counter is:55
server receive order:QUERY TIME ORDER;the counter is:56
server receive order:QUERY TIME ORDER;the counter is:57
server receive order:QUERY TIME ORDER;the counter is:58
server receive order:QUERY TIME ORDER;the counter is:59
server receive order:QUERY TIME ORDER;the counter is:60
server receive order:QUERY TIME ORDER;the counter is:61
server receive order:QUERY TIME ORDER;the counter is:62
server receive order:QUERY TIME ORDER;the counter is:63
server receive order:QUERY TIME ORDER;the counter is:64
server receive order:QUERY TIME ORDER;the counter is:65
server receive order:QUERY TIME ORDER;the counter is:66
server receive order:QUERY TIME ORDER;the counter is:67
server receive order:QUERY TIME ORDER;the counter is:68
server receive order:QUERY TIME ORDER;the counter is:69
server receive order:QUERY TIME ORDER;the counter is:70
server receive order:QUERY TIME ORDER;the counter is:71
server receive order:QUERY TIME ORDER;the counter is:72
server receive order:QUERY TIME ORDER;the counter is:73
server receive order:QUERY TIME ORDER;the counter is:74
server receive order:QUERY TIME ORDER;the counter is:75
server receive order:QUERY TIME ORDER;the counter is:76
server receive order:QUERY TIME ORDER;the counter is:77
server receive order:QUERY TIME ORDER;the counter is:78
server receive order:QUERY TIME ORDER;the counter is:79
server receive order:QUERY TIME ORDER;the counter is:80
server receive order:QUERY TIME ORDER;the counter is:81
server receive order:QUERY TIME ORDER;the counter is:82
server receive order:QUERY TIME ORDER;the counter is:83
server receive order:QUERY TIME ORDER;the counter is:84
server receive order:QUERY TIME ORDER;the counter is:85
server receive order:QUERY TIME ORDER;the counter is:86
server receive order:QUERY TIME ORDER;the counter is:87
server receive order:QUERY TIME ORDER;the counter is:88
server receive order:QUERY TIME ORDER;the counter is:89
server receive order:QUERY TIME ORDER;the counter is:90
server receive order:QUERY TIME ORDER;the counter is:91
server receive order:QUERY TIME ORDER;the counter is:92
server receive order:QUERY TIME ORDER;the counter is:93
server receive order:QUERY TIME ORDER;the counter is:94
server receive order:QUERY TIME ORDER;the counter is:95
server receive order:QUERY TIME ORDER;the counter is:96
server receive order:QUERY TIME ORDER;the counter is:97
server receive order:QUERY TIME ORDER;the counter is:98
server receive order:QUERY TIME ORDER;the counter is:99
server receive order:QUERY TIME ORDER;the counter is:100

客户端输出

Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :1
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :2
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :3
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :4
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :5
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :6
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :7
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :8
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :9
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :10
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :11
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :12
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :13
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :14
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :15
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :16
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :17
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :18
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :19
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :20
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :21
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :22
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :23
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :24
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :25
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :26
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :27
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :28
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :29
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :30
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :31
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :32
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :33
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :34
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :35
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :36
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :37
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :38
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :39
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :40
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :41
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :42
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :43
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :44
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :45
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :46
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :47
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :48
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :49
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :50
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :51
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :52
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :53
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :54
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :55
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :56
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :57
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :58
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :59
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :60
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :61
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :62
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :63
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :64
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :65
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :66
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :67
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :68
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :69
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :70
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :71
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :72
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :73
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :74
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :75
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :76
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :77
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :78
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :79
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :80
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :81
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :82
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :83
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :84
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :85
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :86
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :87
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :88
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :89
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :90
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :91
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :92
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :93
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :94
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :95
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :96
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :97
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :98
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :99
Now is : Fri Sep 01 19:48:07 CST 2017 ; the counter is :100

如此看来直接使用netty自带的解码器可以完美解决粘包问题,当然拆包问题也是使用这两个解码器就可以搞定了。非常的方便。

https://blog.csdn.net/linsongbin1/java/article/details/77776455

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,240评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,328评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,182评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,121评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,135评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,093评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,013评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,854评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,295评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,513评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,678评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,398评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,989评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,636评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,801评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,657评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,558评论 2 352