Java NIO框架Netty教程 (六) ChannelBuffer

来源:互联网 发布:php jquery post json 编辑:程序博客网 时间:2024/04/27 18:26

在学字符串消息收发的时候,已经提到过。ChannelBuffer是Netty中非常重要的概念。所有消息的收发都依赖于这个Buffer。我们通过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 two messages as two independent packets, an operating system will not treat them
as two messages but as just a bunch of bytes. Therefore, there is no guarantee that what you read is exactly
what your remote peer wrote. For example, let us assume that the TCP/IP stack of an operating system has
received three packets:
+—–+—–+—–+
| ABC | DEF | GHI |
+—–+—–+—–+
Because of this general property 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 is server-side or client-side, should defrag the received data into one
or more meaningful frames that could be easily understood by the application logic. In case of the example
above, the received data should be framed like the following:
+—–+—–+—–+
| ABC | DEF | GHI |
+—–+—–+—–+


 


不知道您理解了没,简单翻译一下就是说。在TCP/IP这种基于流传递的协议中。他识别的不是你每一次发送来的消息,不是分包的。而是,只认识一个整体的流,即使分三次分别发送三段话:ABC、DEF、GHI。在传递的过程中,他就是一个具有整体长度的流。在读流的过程中,如果我一次读取的长度选择的不是三个,我可以收到类似AB、CDEFG、H、I这样的信息。这显然是我们不想看到的。所以说,在你写的消息收发的系统里,需要预先定义好这种解析机制,规定每帧(次)读取的长度。通过代码来理解一下:

 

view sourceprint?
01./**
02.* @author lihzh
03.* @alia OneCoder
04.* @blog http://www.it165.net
05.*/
06.public class ServerBufferHandler extends SimpleChannelHandler {
07. 
08./**
09.* 用户接受客户端发来的消息,在有客户端消息到达时触发
10.*
11.* @author lihzh
12.* @alia OneCoder
13.*/
14.@Override
15.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 lihzh
03.* @alia OneCoder
04.* @blog http://www.it165.net
05.*/
06.public class ClientBufferHandler extends SimpleChannelHandler {
07. 
08./**
09.* 当绑定到服务端的时候触发,给服务端发消息。
10.*
11.* @alia OneCoder
12.* @author lihzh
13.*/
14.@Override
15.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 e
27.*            Netty事件
28.* @author lihzh
29.*/
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 str
43.*            待转换字符串,要求非null
44.* @return 转换后的ChannelBuffer
45.* @author lihzh
46.*/
47.private ChannelBuffer tranStr2Buffer(String str) {
48.ChannelBuffer buffer = ChannelBuffers.buffer(str.length());
49.buffer.writeBytes(str.getBytes());
50.return buffer;
51.}
52. 
53.}

服务端输出结果:

Hello
, I'm
 clie
nt.

这里其实,服务端是否分段发送并不会影响输出结果,也就是说,你一次性的把"Hi, I'm client."这段信息发送过来,输出的结果也是一样的。这就是开头说的,传输的是流,不分包。而只在于你如何分段读写。

0 0
原创粉丝点击