欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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 作为底层支持,具有高性能,异常可控的优点。

二、原理图

Apache MINA框架使用介绍

三、下载jar包

https://www.apache.org/dyn/closer.lua/mina/mina/2.1.3/apache-mina-2.1.3-bin.zip
Apache MINA框架使用介绍

Apache MINA框架使用介绍

四、代码实现

服务器

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

相关标签: MINA Apache Java