mina发送数据

http://www.iteye.com/problems/46667

Mina发送数据参考代码:

 

package com.nari.mina.client;

import java.io.IOException;
import java.net.InetSocketAddress;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.future.IoFutureListener;
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.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import com.nari.mina.server.MyProtocalPack;

public class myChatClient2 {
    
    private static final String HOST = "localhost";     
    private static final int PORT = 2500;     
    static long counter = 0;     
    final static int FC1 = 100;     
    static long start = 0;     
    /**   
     * 使用Mina的框架结构进行测试   
     *    
     * @param args   
     */    
    public static void main(String[] args) throws IOException {     
        start = System.currentTimeMillis();     
        IoConnector connector = new NioSocketConnector();   
        LoggingFilter lf = new LoggingFilter();     
    
        connector.getFilterChain().addLast("logger", lf);     
        connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory()));  
       connector.setHandler(new ChatClientHandler2());  
        connector.getSessionConfig().setUseReadOperation(true);
        connector.getSessionConfig().setReadBufferSize(100);     
        connector.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);      
        ConnectFuture connFuture = connector.connect(new InetSocketAddress(HOST, PORT));     
        connFuture.addListener(new IoFutureListener<ConnectFuture>() {     
            public void operationComplete(ConnectFuture future) {     
                try {     
                    if (future.isConnected()) {     
                        IoSession session = future.getSession();   
                       
                        sendData(session);       
                    } else {     
                        System.out.println("连接不存在 ");     
                    }     
                } catch (Exception e) {     
                    e.printStackTrace();     
                }     
            }     
        });     
        System.out.println("start client ...");    
          
    }     
    public static void sendData(IoSession session) throws IOException {     
        for (int i = 0; i < 100; i++) {     
            String content = "afdjkdafk张新波测试" + i;     
            MyProtocalPack pack = new MyProtocalPack((byte) i, content);     
            session.write(content);     
            System.out.println("send data:" + content);     
        }     
    }     

}
 

 

你可能感兴趣的:(Mina)