Mina学习(1):mina实现简单服务端与客户端

  • mina是一个基于javaNio网络通信应用框架,使用mina可以轻松的搭建服务器,接下来将使用mina搭建一个小型的服务端

  • jar下载: http://download.csdn.net/detail/c1481118216/9645854
    Mina学习(1):mina实现简单服务端与客户端_第1张图片

  • 源代码–MinaServer.java
package server;

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

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

public class MinaServer {
    private static int BUFFSIZE = 1024;
    private static int PORT = 7080; //端口号
    private static IoAcceptor acceptor = null;

    public static void main(String[] args) {
        acceptor = new NioSocketAcceptor();
        //设置编码过滤器
        acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(
                new TextLineCodecFactory(Charset.forName("UTF-8"),
                        LineDelimiter.WINDOWS.getValue(),
                        LineDelimiter.WINDOWS.getValue())));
        //设置读缓冲区大小
        acceptor.getSessionConfig().setReadBufferSize(BUFFSIZE);
        //设置进入空闲状态的时间 10毫秒
        acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
        //设置Handle  业务处理类
        acceptor.setHandler(new MinaHandle());
        //绑定端口号并启动服务器
        try {
            acceptor.bind(new InetSocketAddress(PORT));
        } catch (IOException e) {
//          e.printStackTrace();
        }
    }

}
  • 源代码–MinaHandle.java
package server;

import java.util.Date;

import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

public class MinaHandle implements IoHandler {

    @Override
    public void exceptionCaught(IoSession arg0, Throwable arg1) throws Exception {
        System.out.println("exceptionCaught");

    }

    @Override
    public void inputClosed(IoSession arg0) throws Exception {
        System.out.println("inputClosed");

    }

    @Override
    public void messageReceived(IoSession sesion, Object msg) throws Exception {
        System.out.println("接收到:"+(String)msg);
        //如果接收到exit就关闭连接
        if("exit".equals((String)msg)){
            sesion.close();
        }
        else{
        Date date = new Date();
        sesion.write("\n当前系统时间"+date+"\n");
        }



    }

    @Override
    public void messageSent(IoSession arg0, Object arg1) throws Exception {
        System.out.println("messageSent");

    }

    @Override
    public void sessionClosed(IoSession arg0) throws Exception {
        System.out.println("sessionClosed");

    }

    @Override
    public void sessionCreated(IoSession arg0) throws Exception {
        System.out.println("sessionCreated");

    }

    @Override
    public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception {
        System.out.println("sessionIdle");

    }

    @Override
    public void sessionOpened(IoSession arg0) throws Exception {
        System.out.println("sessionOpened");

    }

}
  • 通过telnet访问服务器,每当接收到客户端发送的消息,便会返回系统当前时间,当接收到exit则关闭连接:

链接:
telnet
会话:
Mina学习(1):mina实现简单服务端与客户端_第2张图片

你可能感兴趣的:(mina2.0)