第一篇: mina框架初了解

一.mima简介

     Apache MINA(Multipurpose Infrastructure for Network Applications) 是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络应用程序提供了非常便利的框架。当前发行的 MINA 版本支持基于 Java NIO 技术的TCP/UDP 应用程序开发、串口通讯程序。

二。demo

首先引入相关jar包
	<dependency>
		<groupId>org.apache.mina</groupId>
		<artifactId>mina-core</artifactId>
		<version>2.0.0-M1</version>
	</dependency>

服务端代码:

public class MinaServer {

	 
    private static int PORT = 6001;
 
    public static void main(String[] args) {
        IoAcceptor acceptor = null;   // 创建连接
        try {
            // 创建一个非阻塞的server端的Socket
            acceptor = new NioSocketAcceptor();
            // 设置过滤器(使用Mina提供的文本换行符编解码器)
            acceptor.getFilterChain().addLast(//添加消息过滤器,以换行符为一段数据结尾标志
                    "codec",
                    new ProtocolCodecFilter(new TextLineCodecFactory(Charset
                            .forName("UTF-8"),
                            LineDelimiter.WINDOWS.getValue(),
                            LineDelimiter.WINDOWS.getValue())));
            // 设置读取数据的缓冲区大小
            acceptor.getSessionConfig().setReadBufferSize(2048);
            // 读写通道10秒内无操作进入空闲状态
            acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
            // 绑定逻辑处理器
            acceptor.setHandler(new ServerHandler()); // 添加业务处理,自己定义的处理类
            // 绑定端口
            acceptor.bind(new InetSocketAddress(PORT));
            System.out.println("服务端启动成功...     端口号为:" + PORT);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
	
}

数据处理类ServerHandler:

public class ServerHandler extends IoHandlerAdapter {
	@Override
    public void sessionCreated(IoSession session) throws Exception {
        System.out.println("服务端与客户端创建连接...");
    }
 
    @Override
    public void sessionOpened(IoSession session) throws Exception {
        System.out.println("服务端与客户端连接打开...");
    }
 
    @Override
    public void messageReceived(IoSession session, Object message)
            throws Exception {
        String msg = message.toString();
        System.out.println("服务端接收到的数据为:" + msg);
        if ("bye".equals(msg)) { // 服务端断开连接的条件
            session.close();
        }
        Date date = new Date();
        session.write(date);
    }
    @Override
    public void messageSent(IoSession session, Object message) throws Exception {
        System.out.println("服务端发送信息成功...");
    }
    @Override
    public void sessionClosed(IoSession session) throws Exception {
 
    }
//    @Override
//    public void sessionIdle(IoSession session, IdleStatusstatus)
//            throws Exception {
//        System.out.println("服务端进入空闲状态...");
//    }
    @Override
    public void exceptionCaught(IoSession session, Throwable cause)
            throws Exception {
//    	System.out.print("服务端异常...", cause);
    }
	
}
--------------------------------------------------------------------------------



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值