I've already mentioned it when I'm learning string messaging (http://www.it165.net/pro/html/201207/3174.html) . Channelbuffer is a very important concept in Netty. All messages are sent and received depending on the buffer. We look at the flow-based messaging mechanism through the official documentation of Netty.
In a stream-based transport such as TCP/IP, received data is stored into a socket receive buffer.
Unfortunately, the buffer of a stream-based transport is not a queue of packets but a queue of bytes. It
means, even if you sent and messages as, independent packets, an operating system would not treat them
As-messages but as just a bunch of bytes. Therefore, there is no guarantee so what are read is exactly
What your remote peer wrote. For example, let us assume, the TCP/IP stack of an operating system have
Received three packets:
+-–+-–+-–+
| ABC | DEF | GHI |
+-–+-–+-–+
Because of a stream-based protocol, there ' s high chance of reading them in the
Following fragmented form in your application:
+--+ ——-+-+-+
| AB | CDEFG | H | I |
+--+ ——-+-+-+
Therefore, a receiving part, regardless it's server-side or client-side, should defrag the received data into one
or more meaningful frames that could is easily understood by the application logic. In case of the example
Above, the received data should is framed like the following:
+-–+-–+-–+
| ABC | DEF | GHI |
+-–+-–+-–+
I do not know if you understand, a simple translation is to say. In a stream-based protocol such as TCP/IP. He doesn't recognize you every time you send a message, it's not a subcontract. Instead, you only know the flow of a whole, even if you send three paragraphs in three separate words: ABC, DEF, GHI. In the process of passing, he is a stream with an overall length. In the process of reading the stream, if I read the length of the selection is not three, I can receive a similar to AB, CDEFG, H, I such information. This is obviously what we don't want to see. So, in the system you write the message to send and receive, you need to pre-define this parsing mechanism, which specifies the length of each frame (times) read. Take a look at the code to understand:
View Sourceprint?01./**02.* @author lihzh03.* @alia OneCoder04.* @blog http://www.it165.net05.*/06.public class ServerBufferHandler extends SimpleChannelHandler {07. 08./**09.* 用户接受客户端发来的消息,在有客户端消息到达时触发10.*11.* @author lihzh12.* @alia OneCoder13.*/14.@Override15.public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {16.ChannelBuffer buffer = (ChannelBuffer) e.getMessage();17.// 五位读取18.while (buffer.readableBytes() >= 5) {19.ChannelBuffer tempBuffer = buffer.readBytes(5);20.System.out.println(tempBuffer.toString(Charset.defaultCharset()));21.}22.// 读取剩下的信息23.System.out.println(buffer.toString(Charset.defaultCharset()));24.}25. 26.}
View Sourceprint?01./**02.* @author lihzh03.* @alia OneCoder04.* @blog http://www.it165.net05.*/06.public class ClientBufferHandler extends SimpleChannelHandler {07. 08./**09.* 当绑定到服务端的时候触发,给服务端发消息。10.*11.* @alia OneCoder12.* @author lihzh13.*/14.@Override15.public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {16.// 分段发送信息17.sendMessageByFrame(e);18.}19. 20./**21.* 将<b>"Hello, I‘m client."</b>分成三份发送。 <br>22.* Hello, <br>23.* I‘m<br>24.* client.<br>25.*26.* @param e27.* Netty事件28.* @author lihzh29.*/30.private void sendMessageByFrame(ChannelStateEvent e) {31.String msgOne = "Hello, ";32.String msgTwo = "I‘m ";33.String msgThree = "client.";34.e.getChannel().write(tranStr2Buffer(msgOne));35.e.getChannel().write(tranStr2Buffer(msgTwo));36.e.getChannel().write(tranStr2Buffer(msgThree));37.}38. 39./**40.* 将字符串转换成{@link ChannelBuffer},私有方法不进行字符串的非空判断。41.*42.* @param str43.* 待转换字符串,要求非null44.* @return 转换后的ChannelBuffer45.* @author lihzh46.*/47.private ChannelBuffer tranStr2Buffer(String str) {48.ChannelBuffer buffer = ChannelBuffers.buffer(str.length());49.buffer.writeBytes(str.getBytes());50.return buffer;51.}52. 53.}
Server Output results:
Hello
, I ' m
Clie
Nt.
In fact, whether or not the service side sends a fragment will not affect the output, that is, you put the "Hi, I ' m client." This message is sent over, the output is the same. This is the beginning, the transmission is the flow, not subcontracting. It's just how you read and write in sections.
Java NIO Framework Netty Tutorial (iv) Channelbuffer