public class TestLengthFieldDecorder {
public static void main(String[] args) {
//这里相当于服务端接收数据
EmbeddedChannel embeddedChannel = new EmbeddedChannel(
new LengthFieldBasedFrameDecoder(1024, 0,
4, 0, 0),
new LoggingHandler(LogLevel.DEBUG)
);
//这里相当于客户端发送数据
ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
send(buffer, "Hello, World");
send(buffer, "Hi!");
embeddedChannel.writeInbound(buffer);
}
private static void send(ByteBuf buf, String content) {
byte[] bytes = content.getBytes();
int length = bytes.length;
buf.writeInt(length);//int代表四个字节
buf.writeBytes(bytes);
}
}
应该把数据中的长度剥离掉
new