mina的小例子

服务端:

package mina;

import org.apache.log4j.Logger;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
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.NioSocketAcceptor;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

/**
 * Created with IntelliJ IDEA.
 * User: zhangyong
 * Date: 13-4-13
 * Time: 下午4:26
 * To change this template use File | Settings | File Templates.
 */
public class DemoServer {

    private static Logger logger = Logger.getLogger(DemoServer.class);

    private static int PORT = 3005;

    public static void main(String[] args) {
        //创建非阻塞的server端的socket
        IoAcceptor acceptor = null;
        try {
            acceptor = new NioSocketAcceptor();
            // 设置日志过滤器
            acceptor.getFilterChain().addLast("logger",new LoggingFilter());
            acceptor.getFilterChain().addLast(   //添加消息过滤器
                    "codec",
                    //Mina自带的根据文本换行符编解码的TextLineCodec过滤器 看到\r\n就认为一个完整的消息结束了
                    new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"),
                            LineDelimiter.WINDOWS.getValue(),
                            LineDelimiter.WINDOWS.getValue()
                    )));
            //设置读取数据的缓冲区的大小
            acceptor.getSessionConfig().setReadBufferSize(2048);
            //读取通道10s内无操作进入空闲状态
            acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
            //绑定逻辑处理器
            acceptor.setHandler(new Demo1ServerHandler());
            //绑定端口
            acceptor.bind(new InetSocketAddress(PORT));
            logger.info("服务器启动成功.. 端口号为:" + PORT);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
package mina;

import org.apache.log4j.Logger;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: zhangyong
 * Date: 13-4-13
 * Time: 下午6:21
 * To change this template use File | Settings | File Templates.
 */
public class Demo1ServerHandler extends IoHandlerAdapter {

    public static Logger logger = Logger.getLogger(Demo1ServerHandler.class);

    public void sessionCreated(IoSession session) throws Exception {
        logger.info("服务端与客户端创建连接");
    }

    public void sessionOpened(IoSession session) throws Exception {
        logger.info("服务器与客户端连接打开...");
    }

    public void messageReceived(IoSession session, java.lang.Object message) throws Exception {
        String msg = message.toString();
        logger.info("服务端接收的数据为:" + msg);
        if ("bye".equals(msg)) {//服务器断开的条件
            session.close(true);
        }
        Date date = new Date();
        session.write(date);
    }

    public void messageSent(IoSession session, Object message) throws Exception {
        logger.info("服务端发送消息成功...");
    }

    public void sessionIdle(IoSession session, IdleStatus status) throws java.lang.Exception {
        logger.error("服务端进入空闲状态...");
    }

    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
        logger.error("服务端发送异常...", cause);
    }

    public void sessionClosed(IoSession session) throws Exception {
        logger.info("关闭连接...");
    }

}
客户端:

package mina;

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.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.transport.socket.nio.NioSocketConnector;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

/**
 * mina 客户端
 * User: zhangyong
 * Date: 13-4-13
 * Time: 下午10:05
 * To change this template use File | Settings | File Templates.
 */
public class MinaClient0 {

    private static Logger logger = Logger.getLogger(MinaClient0.class);

    private static String HOST = "127.0.0.1";

    private static int PORT = 3005;

    public static void main(String[] args) {
        //创建一个非组设的客户端客户端
        IoConnector connector = new NioSocketConnector();
        //设置链接超时时间
        connector.setConnectTimeoutMillis(30000);
        //添加过滤器
        connector.getFilterChain().addLast(   //添加消息过滤器
                "codec",
                //Mina自带的根据文本换行符编解码的TextLineCodec过滤器 看到\r\n就认为一个完整的消息结束了
                new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"),
                        LineDelimiter.WINDOWS.getValue(),
                        LineDelimiter.WINDOWS.getValue()
                )));
        //添加业务逻辑处理器类
        connector.setHandler(new Demo1ClientHandler());
        IoSession session = null;
        try {
            ConnectFuture future = connector.connect(new InetSocketAddress(HOST, PORT));
            future.awaitUninterruptibly(); //等待连接创建完成
            session = future.getSession();
            session.write("我爱你mina");
        } catch (Exception e) {
            logger.error("客户端链接异常...", e);
        }

        session.getCloseFuture().awaitUninterruptibly();
        logger.info("我们要关闭了");
        connector.dispose();
    }

}
package mina;

import org.apache.log4j.Logger;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

/**
 * Created with IntelliJ IDEA.
 * User: zhangyong
 * Date: 13-4-14
 * Time: 上午10:07
 * To change this template use File | Settings | File Templates.
 */
public class Demo1ClientHandler extends IoHandlerAdapter {

    private static Logger logger = Logger.getLogger(Demo1ClientHandler.class);

    public void messageReceived(IoSession session, Object message)
            throws Exception {
        String msg = message.toString();
        logger.info("客户端接收到的信息为:" + msg);
    }

    @Override
    public void exceptionCaught(IoSession session, Throwable cause)
            throws Exception {
        logger.error("客户端发生异常...", cause);
    }

}


你可能感兴趣的:(Mina)