Apache MINA框架使用介绍
程序员文章站
2022-04-15 17:50:15
一、为什么要用MINA框架Apache Mina是一个网络通信应用框架,是基于TCP/IP、UDP/IP协议栈的通信框架,Mina 的异步IO 默认使用的是JAVA NIO 作为底层支持,具有高性能,异常可控的优点。二、原理图三、下载jar包https://www.apache.org/dyn/closer.lua/mina/mina/2.1.3/apache-mina-2.1.3-bin.zip四、代码实现服务器package test0826.mina;import org.ap...
一、为什么要用MINA框架
Apache Mina是一个网络通信应用框架,是基于TCP/IP、UDP/IP协议栈的通信框架,Mina 的异步IO 默认使用的是JAVA NIO 作为底层支持,具有高性能,异常可控的优点。
二、原理图
三、下载jar包
https://www.apache.org/dyn/closer.lua/mina/mina/2.1.3/apache-mina-2.1.3-bin.zip
四、代码实现
服务器
package test0826.mina; import org.apache.mina.core.service.IoAcceptor; import org.apache.mina.core.service.IoHandlerAdapter; 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.NioSocketAcceptor; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.charset.Charset; /**
* 服务器
* @author : Bei-Zhen
* @date : 2020-08-26 22:39
*/ public class MinaServer { public static void main(String[] args) throws IOException { //1.创建IoService实例 IoAcceptor acceptor = new NioSocketAcceptor(); //2.设置过滤链 日志 编解码 acceptor.getFilterChain().addLast("logger",new LoggingFilter()); acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName("UTF-8")))); //3.绑定IO处理器 acceptor.setHandler(new MyServerHandler()); //4.监听端口 acceptor.bind(new InetSocketAddress(7777)); System.out.println("服务器启动,并监听端口:7777"); } } class MyServerHandler extends IoHandlerAdapter { @Override public void sessionCreated(IoSession session) throws Exception { System.out.println("服务器:会话被新创建..."); session.write("我是服务器...."); super.sessionCreated(session); } @Override public void sessionClosed(IoSession session) throws Exception { System.out.println("服务器:当前会话关闭"); super.sessionClosed(session); } @Override public void sessionIdle(IoSession session, IdleStatus status) throws Exception { System.out.println("服务器:会话休眠中"); super.sessionIdle(session, status); } @Override public void exceptionCaught(IoSession session, Throwable cause) throws Exception { System.out.println("服务器发生异常"); super.exceptionCaught(session, cause); } @Override public void messageReceived(IoSession session, Object message) throws Exception { System.out.println("服务器接收消息:" + message); super.messageReceived(session, message); } @Override public void messageSent(IoSession session, Object message) throws Exception { System.out.println("服务器发送消息:" + message); super.messageSent(session, message); } @Override public void inputClosed(IoSession session) throws Exception { System.out.println("服务器关闭:" + session); super.inputClosed(session); } }
客户端
package test0826.mina; import org.apache.mina.core.future.ConnectFuture; import org.apache.mina.core.service.IoHandlerAdapter; 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 java.net.InetSocketAddress; import java.nio.charset.Charset; /**
* 客户端
* @author : Bei-Zhen
* @date : 2020-08-26 23:04
*/ public class MinaClient { public static void main(String[] args) { //1.创建IoService实例 NioSocketConnector connector = new NioSocketConnector(); //2.设置过滤链 日志 编解码 connector.getFilterChain().addLast("logger",new LoggingFilter()); connector.getFilterChain().addLast("codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName("UTF-8")))); //3.绑定IO处理器 connector.setHandler(new MyClientHandler()); //4.连接地址 ConnectFuture cf = connector.connect( new InetSocketAddress("localhost",7777)); System.out.println("客户端连接成功!"); } } class MyClientHandler extends IoHandlerAdapter { @Override public void sessionCreated(IoSession session) throws Exception { System.out.println("客服端:会话被新创建..."); session.write("我是客户端...."); super.sessionCreated(session); } @Override public void sessionClosed(IoSession session) throws Exception { System.out.println("客服端:当前会话关闭"); super.sessionClosed(session); } @Override public void sessionIdle(IoSession session, IdleStatus status) throws Exception { System.out.println("客服端:会话休眠中"); super.sessionIdle(session, status); } @Override public void exceptionCaught(IoSession session, Throwable cause) throws Exception { System.out.println("客服端发生异常"); super.exceptionCaught(session, cause); } @Override public void messageReceived(IoSession session, Object message) throws Exception { System.out.println("客服端接收消息:" + message); super.messageReceived(session, message); } @Override public void messageSent(IoSession session, Object message) throws Exception { System.out.println("客服端发送消息:" + message); super.messageSent(session, message); } @Override public void inputClosed(IoSession session) throws Exception { System.out.println("客服端关闭:" + session); super.inputClosed(session); } }
五、运行结果
服务器
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. 服务器启动,并监听端口:7777 服务器:会话被新创建... 服务器发送消息:我是服务器.... 服务器接收消息:我是客户端....
客户端
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. 客户端连接成功!
客服端:会话被新创建... 客服端发送消息:我是客户端.... 客服端接收消息:我是服务器....
本文地址:https://blog.csdn.net/qq_33591873/article/details/108249999
推荐阅读
-
Apache Commons DbUtils工具包使用介绍
-
解析PHPExcel使用的常用说明以及把PHPExcel整合进CI框架的介绍
-
iframe使用方法,iframe框架标签详情介绍!
-
Scrapy框架CrawlSpiders的介绍以及使用详解
-
Apache Shiro 使用手册(一) Shiro架构介绍
-
spark学习-72-源代码:Endpoint模型介绍(4)-Spark为何使用Netty通信框架替代Akka
-
Curator框架简单介绍和使用
-
Curator框架简单介绍和使用
-
Apache Mina TCP/UDP功能使用说明
-
APACHE的AcceptPathInfo指令使用介绍