Search in sources :

Example 1 with IoConnector

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();
}
Also used : NioSocketConnector(org.apache.mina.transport.socket.nio.NioSocketConnector) InetSocketAddress(java.net.InetSocketAddress) ConnectFuture(org.apache.mina.core.future.ConnectFuture) IoConnector(org.apache.mina.core.service.IoConnector) ProtocolCodecFilter(org.apache.mina.filter.codec.ProtocolCodecFilter) IoSession(org.apache.mina.core.session.IoSession)

Example 2 with IoConnector

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;
}
Also used : NioSocketConnector(org.apache.mina.transport.socket.nio.NioSocketConnector) MinaClientHandler(com.pancm.test.minatest.demo.MinaClientHandler) ObjectSerializationCodecFactory(org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory) IoConnector(org.apache.mina.core.service.IoConnector) ProtocolCodecFilter(org.apache.mina.filter.codec.ProtocolCodecFilter)

Example 3 with IoConnector

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());
    }
}
Also used : IoConnector(org.apache.mina.core.service.IoConnector) IoSession(org.apache.mina.core.session.IoSession)

Aggregations

IoConnector (org.apache.mina.core.service.IoConnector)3 IoSession (org.apache.mina.core.session.IoSession)2 ProtocolCodecFilter (org.apache.mina.filter.codec.ProtocolCodecFilter)2 NioSocketConnector (org.apache.mina.transport.socket.nio.NioSocketConnector)2 MinaClientHandler (com.pancm.test.minatest.demo.MinaClientHandler)1 InetSocketAddress (java.net.InetSocketAddress)1 ConnectFuture (org.apache.mina.core.future.ConnectFuture)1 ObjectSerializationCodecFactory (org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory)1