3.12 mina简单tcp使用

来源:互联网 发布:软件压力测试报告模板 编辑:程序博客网 时间:2024/06/12 18:09

http<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">://blog.csdn.net/ljx8928358/article/details/7759024</span>

http://blog.csdn.net/cnicys/article/details/8535485




session就像mina框架的心脏,每一个client的连接到达server之后都会创建一个新的session,一直保存在内存单元中直到client 连接断开。

session 常常用来储存一些和连接相关的持久化信息,这些信息在server 进行处理的时候都是非常有用的,生命周期为整个会话

1、session的状态

通常来讲 mina 的session 有以下几个状态

1、Connected : client 连接上server 之后,这时 session 已经建立,并且该session 是有效的、可用的

2、Idle   : 此时 session并没有处理任何的请求,并且这个时间超过了你设定的 session 进入空闲状态的那个时间点,idle又细分为三个状态

  (1) idle for read  超过读通道设定的空闲时间

  (2) idle for write 超过写通道设定的空闲时间

  (3) idle for both  读写通道均超过设定空闲时间

3、Closing : 这时候session即将被关闭但是有没有完全关闭(对余下未处理完的消息进行 flushed 操作)

4、Closed : session已经完全关闭,并且这个状态时不可逆的







android客户端的使用:

//使用的CPU核心输connector = new NioSocketConnector(Runtime.getRuntime().availableProcessors() + 1);//设置为非延迟发送,为true则不组装成大包发送,收到东西马上发出     connector.getSessionConfig().setTcpNoDelay(true);//设置输入缓冲区的大小  connector.getSessionConfig().setReceiveBufferSize(Global.SOCKET.READ_BUFFER_SIZE);//设置输出缓冲区的大小  connector.getSessionConfig().setSendBufferSize(Global.SOCKET.READ_BUFFER_SIZE); //读写 通道均在10 秒内无任何操作就进入空闲状态 connector.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 130);//数据处理类connector.getFilterChain().addLast("codec",new ProtocolCodecFilter(new SimpleEncoder(),new SimpleDecoder()));//处理连接状态,返回处理后数据connector.setHandler(new ServerSessionHandler());//设置ip端口connector.setDefaultRemoteAddress(new InetSocketAddress(ip, port));ConnectFuture future = connector.connect();

处理连接:

class ServerSessionHandler extends IoHandlerAdapter { }

处理解析:

public class SimpleDecoderextends ProtocolDecoderAdapter {}

public class SimpleEncoderimplements ProtocolEncoder {}

发送一个数据:


public void encode(IoSession session, Object obj, ProtocolEncoderOutput out)throws Exception { 

out.write("hello!");

}

IoSession session

session.write(param);







0 0
原创粉丝点击