使用mina的TextLineDecoder,可以指定接收数据的结束符号,但如果没有指定结束符,就默认会去匹配'/n'或者'/r'结束符
如果从服务端返回的数据没有包含这些结束符,客户端就会等待。
为了不让客户端等待下去,可以增加一个TextLineDecoder子类,如下:
public class ClientTextLineDecoder extends TextLineDecoder {
@Override
public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out)
throws Exception {
/*--------在当前buffer后面追加一个'/n' -------*/
in.setAutoExpand(true);//设置自动扩展
in.position(in.limit());
in.put((byte)'/n');
in.position(0);
super.decode(session, in, out);
}
}
我们只要在调用父类的decode方法之前在buffer后面追加一个'/n'结束符,就可以解决问题。
下面是buffer相关的知识:
Buffer foundations
The abstract Buffer
class forms the foundation of the java.nio
package's buffer support. Buffer
works like an in-memory RandomAccessFile
for reading and writing primitive data types. Like a RandomAccessFile
,
with Buffer
the next operation you perform (read/write)
happens at your position. Performing either operation changes that
position, so doing a read after a write doesn't read what was just
written, but what is after what was just written. Buffer
offers four indicators
for access to the linear structure (from highest value to lowest):
-
capacity()
: Indicates the buffer's size
-
limit()
: Tells you how many bytes you've stuffed into
the buffer so far, or lets you change that limit with :limit(int
newLimit)
:
-
position()
: Tells you the current location to perform
the next read/write operation
-
mark()
: Lets you remember a position for later
resetting with reset()
具体可以参考:http://www.ibm.com/developerworks/java/library/j-mer03253.html
分享到:
相关推荐
mina连接,mina心跳连接,mina断线重连。其中客户端可直接用在android上。根据各方参考资料,经过自己的理解弄出来的。CSDN的资源分太难得了。
mina-core-2.0.0-M6.jar mina-example-2.0.0-M6.jar mina-filter-codec-netty-2.0.0-M6.jar mina-filter-compression-2.0.0-M6.jar mina-integration-beans-2.0.0-M6.jar mina-integration-jmx-2.0.0-M6.jar mina-...
mina的使用初步入门mina的使用初步入门mina的使用初步入门
里面包含mina2.0的api(英文)和mina自学手册,还有mina的开发指导
Apache MINA是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络应用程序提供了非常便利的框架。 当前发行的 MINA 版本支持基于 Java NIO 技术的 TCP/UDP 应用程序开发、串口通讯程序(只在最新的预览版...
Apache Mina Server 2.0中文参考手册V1.0,Apache Mina2.0学习笔记(修订版)Apache Mina Server 2.0中文参考手册V1.0,Apache Mina2.0学习笔记(修订版)
mina的高级使用,mina文件图片传送,
深入理解Apache_Mina_(1)----_Mina的几个类 深入理解Apache_Mina_(2)----_与IoFilter相关的几个类 深入理解Apache_Mina_(3)----_与IoHandler相关的几个类 深入理解Apache_Mina_(4)----_IoFilter和IoHandler的区别和...
mina内部源码,可以深入的研究下,重构修改后获得的效率更加突出
mina新手案例,mina新手教程源码 mina+springboot最简单的案例。用的IDEA * mina服务端 * 1、添加@Controller注解和 @PostConstruct注解,代表启动springboot项目时也调用该类下的该方法, * 启动springboot项目...
许多刚接触mina的朋友,对于mina的编解码器的编写很迷惑.希望这个文档可以帮助朋友们少走弯路。 资源中是一个比较典型的编解码器写法。生成了可执行文件。并对编解码器的代码有详细注释。
1.mina socket客户度工程相关类,添加mina jar包后可独立运行。 2.mina若有空闲连接则使用已有连接,若无则新建mina连接; 3.mina空闲连接超过保活时间25分钟后,自动删除; 4.mina发送指令后,接收指定时长内收到的...
mina客户端,服务器端的demo
项目包含有mina的服务端与客户端,客户端发送心跳包,服务端响应心跳包
mina.jar 包,内涵mina所需的所有jar包, 解压即可,测试可用。mina.jar 包,内涵mina所需的所有jar包, 解压即可,测试可用。
MINA 2.0 User Guide Part I - Basics Chapter 1 - Getting Started Chapter 2 - Basics Chapter 3 - Service Chapter 4 - Session Chapter 5 - Filters Chapter 6 - Transports Chapter 7 - Handler Part II - ...
mina运行最基本的demo,实现客户端输入数据的返回。其中还包含mina的jar包
mina mina传输对象的示例 mina框架 mina示例
两个mina开发文档,含有所有的mina的简单开发案例与教程
本库是对我在项目中使用的Mina和长连接的一个封装,亲测有效,在网络良好的情况下,几乎能够保证100%的连接和通讯;