use of org.apache.mina.core.service.IoConnector in project pancm_project by xuwujing.
the class MinaClient method main.
/**
* The entry point of application.
*
* @param args the input arguments
*/
/*
* 测试服务端与客户端程序!
a. 启动服务端,然后再启动客户端(客户端发送的消息是"why are you so diao ")
b. 服务端接收消息并处理成功;
*/
public static void main(String[] args) {
// 创建一个非阻塞的客户端程序
IoConnector connector = new NioSocketConnector();
// 设置链接超时时间
connector.setConnectTimeout(30000);
ProtocolCodecFilter pf = new ProtocolCodecFilter((new MyTextLineCodecFactory(Charset.forName("utf-8"), "\r\n")));
// 添加过滤器
connector.getFilterChain().addLast("codec", pf);
// 添加业务逻辑处理器类
connector.setHandler(new MinaClientHandler());
IoSession session = null;
try {
ConnectFuture future = connector.connect(new InetSocketAddress(HOST, // 创建连接
PORT));
// 等待连接创建完成
future.awaitUninterruptibly();
// 获得session
session = future.getSession();
String msg = "hello \r\n";
// 发送消息
session.write(msg);
logger.info("客户端与服务端建立连接成功...发送的消息为:" + msg);
} catch (Exception e) {
e.printStackTrace();
logger.error("客户端链接异常...", e);
}
// 等待连接断开
session.getCloseFuture().awaitUninterruptibly();
connector.dispose();
}
use of org.apache.mina.core.service.IoConnector in project pancm_project by xuwujing.
the class ClientTestServer method creatClient.
/**
* Creat client io connector.
*
* @return the io connector
*/
public IoConnector creatClient() {
IoConnector connector = new NioSocketConnector();
connector.setConnectTimeoutMillis(30000);
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
connector.setHandler(new MinaClientHandler());
return connector;
}
use of org.apache.mina.core.service.IoConnector in project pancm_project by xuwujing.
the class ClientTestServer method main.
/**
* The entry point of application.
*
* @param args the input arguments
*/
public static void main(String[] args) {
for (int i = 0; i < 4000; i++) {
ClientTestServer client = new ClientTestServer();
IoConnector connector = client.creatClient();
IoSession session = client.getIOSession(connector);
client.sendMsg(session, Arrays.toString(new byte[1000]) + ":" + System.currentTimeMillis());
}
}
Aggregations