-
在学字符串消息收发(http://www.it165.net/pro/html/201207/3174.html)的时候,已经提到过。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这样的信息。这显然是我们不想看到的。所以说,在你写的消息收发的系统里,需要预先定义好这种解析机制,规定每帧(次)读取的长度。通过代码来理解一下:
01./**02.* @author lihzh03.* @alia OneCoder04.* @blog http://www.it165.net05.*/06.publicclassServerBufferHandlerextendsSimpleChannelHandler {07.08./**09.* 用户接受客户端发来的消息,在有客户端消息到达时触发10.*11.* @author lihzh12.* @alia OneCoder13.*/14.@Override15.publicvoidmessageReceived(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.}
01./**02.* @author lihzh03.* @alia OneCoder04.* @blog http://www.it165.net05.*/06.publicclassClientBufferHandlerextendsSimpleChannelHandler {07.08./**09.* 当绑定到服务端的时候触发,给服务端发消息。10.*11.* @alia OneCoder12.* @author lihzh13.*/14.@Override15.publicvoidchannelConnected(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.privatevoidsendMessageByFrame(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.privateChannelBuffer tranStr2Buffer(String str) {48.ChannelBuffer buffer = ChannelBuffers.buffer(str.length());49.buffer.writeBytes(str.getBytes());50.returnbuffer;51.}52.53.}
服务端输出结果:
Hello
, I'm
clie
nt.
这里其实,服务端是否分段发送并不会影响输出结果,也就是说,你一次性的把"Hi, I'm client."这段信息发送过来,输出的结果也是一样的。这就是开头说的,传输的是流,不分包。而只在于你如何分段读写。
阅读下一篇:Java NIO框架Netty教程(五) 消息收发次数不匹配的问题 http://www.it165.net/pro/html/201207/3219.html
Java NIO框架Netty教程(四) ChannelBuffer
最新推荐文章于 2022-11-14 11:49:01 发布
本文深入探讨了Netty中ChannelBuffer在消息收发中的应用,解释了基于流的传输机制如何处理消息分包与重组问题,并提供了基于流的协议下消息收发的代码实现案例。
429

被折叠的 条评论
为什么被折叠?



