apache mina发送数据后session立即就关闭了
apache mina发送数据后session立即就关闭了,但是我想它返回数据后才关闭,这个是什么回事啊...IoConnector connector = new NioSocketConnector();
connector.getFilterChain().addLast("protocol",
new ProtocolCodecFilter(new MeysLoginCodeFactory(1)));
connector.setHandler(new MeysLoginHandler());
connector.getSessionConfig().setReadBufferSize(2048);
// 获取session
IoSession session;
for (;;) {
try {
ConnectFuture future = connector
.connect(Config.loginServerConfig
.get(Constants.MEYS_GAME_NAME));
future.awaitUninterruptibly();
session = future.getSession();
break;
} catch (RuntimeIoException e) {
System.err.println("Failed to connect.");
e.printStackTrace();
}
}
MeysLoginRequest loginRequest = MeysLoginRequest
.builder(user);
session.write(loginRequest); //write完后session就close了,
--------------------编程问答--------------------
package com.mina.test.client;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import org.apache.log4j.Logger;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
public class TestClient {
public static Logger logger = Logger.getLogger(TestClient.class);
private static String HOST = "127.0.0.1";
private static int Prot = 3005;
public static void main(String[] args) {
//创建客户端
IoConnector connector = new NioSocketConnector();
//设置连接超时的时间
connector.setConnectTimeoutMillis(30000);
//添加日志管理
connector.getFilterChain().addFirst("logger",new LoggingFilter());
connector.getFilterChain().addFirst("codec",
new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"),
LineDelimiter.WINDOWS.getValue(),LineDelimiter.WINDOWS.getValue())));
//设置关联在通道上的读、写或者是读写事件在指定时间内未发生,该通道就进入空闲状态
//connector.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
connector.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 30);
connector.setHandler(new TestClientHandler());
IoSession session = null;
try{
//创建连接
ConnectFuture future = connector.connect(new InetSocketAddress(HOST,Prot));
//等待连接创建完成 相当于是转异步执行为同步执行。
future.awaitUninterruptibly();
//获得session 如果没有上面的等待,由于connect()方法是异步的,session 可能会无法获取。
session = future.getSession();
}catch (Exception e) {
logger.error("客户端连接异常..."+e);
}
session.getCloseFuture().awaitUninterruptibly();
connector.dispose();
}
}
感觉你的session 发完了 之后肯定有地方close()了 好好检查一下吧
补充:Java , Java SE