nio聊天和使用Mina实现nio聊天:
http://dreamoftch.iteye.com/blog/2087858
server:
package com.tch.test.jms.origin.server; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; 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 org.slf4j.Logger; import org.slf4j.LoggerFactory; public class MyServer { private static final Logger LOGGER = LoggerFactory.getLogger(MyServer.class); private List<IoSession> clientSessionList = new ArrayList<IoSession>(); public static void main(String[] args) { IoAcceptor acceptor = new NioSocketAcceptor(); acceptor.getFilterChain().addLast("logger", new LoggingFilter()); acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8")))); acceptor.setHandler(new MyServer().new MyServerIoHandler()); try { acceptor.bind(new InetSocketAddress(10000)); } catch (IOException ex) { LOGGER.error(ex.getMessage(), ex); } } class MyServerIoHandler extends IoHandlerAdapter{ @Override public void sessionCreated(IoSession session) throws Exception { LOGGER.info("sessionCreated"); } @Override public void sessionOpened(IoSession session) throws Exception { LOGGER.info("sessionOpened"); if(! clientSessionList.contains(session)){ clientSessionList.add(session); } } @Override public void sessionClosed(IoSession session) throws Exception { LOGGER.info("sessionClosed"); clientSessionList.remove(session); } @Override public void sessionIdle(IoSession session, IdleStatus status) throws Exception { LOGGER.info("sessionIdle"); } @Override public void exceptionCaught(IoSession session, Throwable cause) throws Exception { LOGGER.error(cause.getMessage(), cause); session.close(true); clientSessionList.remove(session); } @Override public void messageReceived(IoSession session, Object message) throws Exception { LOGGER.info("messageReceived:" + message); for(IoSession clientSession : clientSessionList){ clientSession.write(message); } } @Override public void messageSent(IoSession session, Object message) throws Exception { LOGGER.info("messageSent:" + message); } } }
client :
package com.tch.test.jms.origin.client; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.charset.Charset; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JTextField; import org.apache.mina.core.RuntimeIoException; import org.apache.mina.core.future.ConnectFuture; import org.apache.mina.core.service.IoConnector; 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 org.slf4j.Logger; import org.slf4j.LoggerFactory; public class NioClient extends JFrame{ private static final Logger LOGGER = LoggerFactory.getLogger(NioClient.class); private static final long serialVersionUID = 1L; private JTextArea area = new JTextArea("content :"); private JTextField textField = new JTextField("textfield:"); private JButton button = new JButton("send"); private String message = null; private MyClientIoHandler handler; private IoSession session; public static void main(String[] args) throws Exception { NioClient client = new NioClient(); client.start(); } private void start() throws IOException{ setBounds(200, 200, 300, 400); setLayout(new GridLayout(3, 1)); add(area); add(textField); IoConnector connector = new NioSocketConnector(); connector.setConnectTimeoutMillis(10 * 1000); connector.getFilterChain().addLast("logger", new LoggingFilter()); connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8")))); handler = new MyClientIoHandler(this); connector.setHandler(handler); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { sendMessage(); } }); add(button); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); IoSession session = null; try { ConnectFuture future = connector.connect(new InetSocketAddress("localhost", 10000)); future.awaitUninterruptibly(); session = future.getSession(); } catch (RuntimeIoException e) { LOGGER.error(e.getMessage(), e); } session.getCloseFuture().awaitUninterruptibly(); connector.dispose(); } private void sendMessage() { try { message = textField.getText(); textField.setText(""); if(session == null || ! session.isConnected()){ throw new RuntimeException("session is null"); } session.write(message); } catch (Exception e) { e.printStackTrace(); } } class MyClientIoHandler extends IoHandlerAdapter{ private NioClient client; public MyClientIoHandler(NioClient client){ this.client = client; } @Override public void sessionCreated(IoSession session) throws Exception { LOGGER.info("sessionCreated"); } @Override public void sessionOpened(IoSession session) throws Exception { LOGGER.info("sessionOpened"); client.session = session; } @Override public void sessionClosed(IoSession session) throws Exception { LOGGER.info("sessionClosed"); } @Override public void sessionIdle(IoSession session, IdleStatus status) throws Exception { LOGGER.info("sessionIdle"); } @Override public void exceptionCaught(IoSession session, Throwable cause) throws Exception { LOGGER.error(cause.getMessage(), cause); session.close(true); } @Override public void messageReceived(IoSession session, Object message) throws Exception { LOGGER.info("messageReceived: " + message); if (message.toString().equalsIgnoreCase("Bye")) { session.close(true); } area.setText(area.getText().trim()+"\n"+message); } @Override public void messageSent(IoSession session, Object message) throws Exception { LOGGER.info("messageSent: " + message); } } }
参考:
MINA用法介绍:
Mina几个主要的组件如下:
I/O Service - 用来处理I/O流,对于Server端就是IOAcceptor的实现类接受Client端的连接请求,对于Client端就是IoConnector的实现类来建立到Server端的连接。
I/O Filter Chain - 用来过滤或转化数据。对于Server端和Client端来说都是IoFilter接口的实现类,MINA自己内建了很多IoFilter接口的实现类。具体可以参考官方文档。
I/O Handler - 用来处理真正业务逻辑的类。对于Server端和Client端来说都是IoHandler接口的实现类,通常来说需要自己编写。
由于Server端和Client端都是基于以上三个组件的,因此对于Server端和Client端编程来说就都有类似的代码结构。
对于Server端来说:
1. 创建I/O service - 这里就是创建IOAcceptor类监听端口。
2. 创建I/O Filter Chain - 这里就是告诉使用那些IoFilter。
3. 创建I/O Handler - 自己的业务逻辑。
对于Client端来说:
1. 创建I/O service - 这里就是创建IOConnector类来建立到Server端的连接。
2. 创建I/O Filter Chain - 这里就是告诉使用那些IoFilter。
3. 创建I/O Handler - 自己的业务逻辑。
log4j.properties :
log4j.rootLogger=info, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %5p (%c:%L) - %m%n log4j.logger.org.apache.mina=ERROR
下面来通过一个例子看看MINA是怎样工作的。由于大多数应用都是基于TCP/IP的应用,所以这里也就不再说UDP/IP了。
这里我使用了Maven来创建了一个简单java应用程序,具体步骤请参Maven的官方手册。这里只是将我用到的maven配置文件pom.xml列出,方便下面及后续文章使用。具体pom.xml文件内容如下:
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.google.code.garbagecan.minastudy</groupId>
- <artifactId>minastudy</artifactId>
- <packaging>jar</packaging>
- <version>1.0-SNAPSHOT</version>
- <name>minastudy</name>
- <url>http://maven.apache.org</url>
- <dependencies>
- <dependency>
- <groupId>org.apache.mina</groupId>
- <artifactId>mina-core</artifactId>
- <version>2.0.4</version>
- </dependency>
- <dependency>
- <groupId>org.apache.mina</groupId>
- <artifactId>mina-filter-compression</artifactId>
- <version>2.0.4</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>1.3.0</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>1.3.0</version>
- </dependency>
- </dependencies>
- </project>
首先来看Server端的代码
- package com.google.code.garbagecan.minastudy.sample1;
- 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.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 org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- public class MyServer {
- private static final Logger logger = LoggerFactory.getLogger(MyServer.class);
- public static void main(String[] args) {
- IoAcceptor acceptor = new NioSocketAcceptor();
- acceptor.getFilterChain().addLast("logger", new LoggingFilter());
- acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
- acceptor.setHandler(new IoHandlerAdapter() {
- @Override
- public void sessionCreated(IoSession session) throws Exception {
- }
- @Override
- public void sessionOpened(IoSession session) throws Exception {
- }
- @Override
- public void sessionClosed(IoSession session) throws Exception {
- }
- @Override
- public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
- }
- @Override
- public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
- logger.error(cause.getMessage(), cause);
- session.close(true);
- }
- @Override
- public void messageReceived(IoSession session, Object message) throws Exception {
- logger.info("Received message " + message);
- session.write(message);
- }
- @Override
- public void messageSent(IoSession session, Object message) throws Exception {
- logger.info("Sent message " + message);
- }
- });
- try {
- acceptor.bind(new InetSocketAddress(10000));
- } catch (IOException ex) {
- logger.error(ex.getMessage(), ex);
- }
- }
- }
1. 首先创建I/O Service,这里使用的是NioSocketAcceptor类来创建了一个IoAcceptor实例。
2. 创建I/O Filter Chain,这里使用了两个IoFilter,一个是LoggingFilter用来记录日志和打印事件消息,另一个是ProtocolCodecFilter实例用来编码数据,这里其实就是将传递的数据编码成文本。
3. 创建I/O Handler,不要害怕,看起来代码多,其实就是一个实现了IoHandler接口的子类,自己需要实现其中的一些方法,这里方法比较多,但是我在这里只实现了messageSent,messageReceived和exceptionCaught方法。
4. 最后就是让IoAcceptor类实例绑定端口实现监听。
下面看看Client端的代码
- package com.google.code.garbagecan.minastudy.sample1;
- import java.net.InetSocketAddress;
- import java.nio.charset.Charset;
- import org.apache.mina.core.RuntimeIoException;
- import org.apache.mina.core.future.ConnectFuture;
- import org.apache.mina.core.service.IoConnector;
- 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 org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- public class MyClient {
- private static final Logger logger = LoggerFactory.getLogger(MyClient.class);
- public static void main(String[] args) {
- IoConnector connector = new NioSocketConnector();
- connector.setConnectTimeoutMillis(10 * 1000);
- connector.getFilterChain().addLast("logger", new LoggingFilter());
- connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
- connector.setHandler(new IoHandlerAdapter() {
- @Override
- public void sessionCreated(IoSession session) throws Exception {
- }
- @Override
- public void sessionOpened(IoSession session) throws Exception {
- for (int i = 0; i < 10; i++) {
- session.write("Hello user_" + i);
- }
- session.write("Bye");
- }
- @Override
- public void sessionClosed(IoSession session) throws Exception {
- }
- @Override
- public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
- }
- @Override
- public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
- logger.error(cause.getMessage(), cause);
- session.close(true);
- }
- @Override
- public void messageReceived(IoSession session, Object message) throws Exception {
- logger.info("Received message " + message);
- if (message.toString().equalsIgnoreCase("Bye")) {
- session.close(true);
- }
- }
- @Override
- public void messageSent(IoSession session, Object message) throws Exception {
- logger.info("Sent message " + message);
- }
- });
- IoSession session = null;
- try {
- ConnectFuture future = connector.connect(new InetSocketAddress("localhost", 10000));
- future.awaitUninterruptibly();
- session = future.getSession();
- } catch (RuntimeIoException e) {
- logger.error(e.getMessage(), e);
- }
- session.getCloseFuture().awaitUninterruptibly();
- connector.dispose();
- }
- }
1. 首先创建I/O Service,这里使用的是NioSocketConnector类来创建了一个IoConnector实例,并设置连接超时为10秒。
2. 创建I/O Filter Chain,和服务器端同样设置了两个IoFilter,一个是LoggingFilter用来记录日志和打印事件消息,另一个是ProtocolCodecFilter实例用来编码数据,这里其实就是将传递的数据编码成文本。
3. 创建I/O Handler,也不要害怕,看起来代码多,其实也是一个实现了IoHandler接口的子类,并且自己实现了sessionOpened,messageSent,messageReceived和exceptionCaught方法。实现sessionOpened方法是为了在建立连接后向Server端发送消息。另外看一下messageReceived方法实现,在接收到服务器端的消息后关闭会话。从而可以使Client程序最终能够退出。
4. 最后就是IoConnector实例类连接远端的Server。
下面测试一下上面的程序,首先运行MyServer类,然后运行MyClient类,就可以分别在各自的终端上看到事件日志以及发送/接收的消息了。
下面是发送对象:
利用Apache MINA来传递对象,这对了MINA来说非常容易,并且这也是Java网络编程中很常用的应用。其实对于MINA传递对象来说,如果看过前一篇文章的话,只要在其中做少许改动就可以实现对象传递,但这里考虑到例子的完整性,还是给出了全部代码示例。
首先看两个用来传递的Java对象MyRequestObject和MyResponseObject,很简单只是实现了Serializable接口罢了。
- package com.google.code.garbagecan.minastudy.sample3;
- import java.io.Serializable;
- public class MyRequestObject implements Serializable {
- private static final long serialVersionUID = 1L;
- private String name;
- private String value;
- public MyRequestObject(String name, String value) {
- this.name = name;
- this.value = value;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getValue() {
- return value;
- }
- public void setValue(String value) {
- this.value = value;
- }
- @Override
- public String toString() {
- StringBuffer sb = new StringBuffer();
- sb.append("Request [name: " + name + ", value: " + value + "]");
- return sb.toString();
- }
- }
- package com.google.code.garbagecan.minastudy.sample3;
- import java.io.Serializable;
- public class MyResponseObject implements Serializable {
- private static final long serialVersionUID = 1L;
- private String name;
- private String value;
- public MyResponseObject(String name, String value) {
- this.name = name;
- this.value = value;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getValue() {
- return value;
- }
- public void setValue(String value) {
- this.value = value;
- }
- @Override
- public String toString() {
- StringBuffer sb = new StringBuffer();
- sb.append("Response [name: " + name + ", value: " + value + "]");
- return sb.toString();
- }
- }
看看Server端的代码
- package com.google.code.garbagecan.minastudy.sample3;
- import java.io.IOException;
- import java.net.InetSocketAddress;
- 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.serialization.ObjectSerializationCodecFactory;
- import org.apache.mina.filter.logging.LoggingFilter;
- import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- public class MyServer {
- private static final Logger logger = LoggerFactory.getLogger(MyServer.class);
- public static void main(String[] args) {
- IoAcceptor acceptor = new NioSocketAcceptor();
- acceptor.getFilterChain().addLast("logger", new LoggingFilter());
- acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
- acceptor.setHandler(new IoHandlerAdapter() {
- @Override
- public void sessionCreated(IoSession session) throws Exception {
- }
- @Override
- public void sessionOpened(IoSession session) throws Exception {
- }
- @Override
- public void sessionClosed(IoSession session) throws Exception {
- }
- @Override
- public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
- }
- @Override
- public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
- logger.error(cause.getMessage(), cause);
- session.close(true);
- }
- @Override
- public void messageReceived(IoSession session, Object message) throws Exception {
- logger.info("Received " + message);
- MyRequestObject myReqOjb = (MyRequestObject) message;
- MyResponseObject myResObj = new MyResponseObject(myReqOjb.getName(), myReqOjb.getValue());
- session.write(myResObj);
- }
- @Override
- public void messageSent(IoSession session, Object message) throws Exception {
- logger.info("Sent " + message);
- }
- });
- try {
- acceptor.bind(new InetSocketAddress(10000));
- } catch (IOException ex) {
- logger.error(ex.getMessage(), ex);
- }
- }
- }
1. 首先创建I/O Service,这里使用的是NioSocketAcceptor类来创建了一个IoAcceptor实例。
2. 创建I/O Filter Chain,这里使用了两个个IoFilter,一个是LoggingFilter用来记录日志和打印事件消息,另一个是ProtocolCodecFilter实例用来编码数据,这里使用了ObjectSerializationCodecFactory类来序列化或反序列化数据成java对象。
3. 创建I/O Handler,这里主要看一下messageReceived方法,其总接收了MyRequestObject对象,然后又发送了一个MyResponseObject对象给Client端。
4. 最后就是让IoAcceptor类实例绑定端口实现监听。
看看Client端的代码
- package com.google.code.garbagecan.minastudy.sample3;
- import java.net.InetSocketAddress;
- import org.apache.mina.core.RuntimeIoException;
- import org.apache.mina.core.future.ConnectFuture;
- import org.apache.mina.core.service.IoConnector;
- 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.serialization.ObjectSerializationCodecFactory;
- import org.apache.mina.filter.logging.LoggingFilter;
- import org.apache.mina.transport.socket.nio.NioSocketConnector;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- public class MyClient {
- private static final Logger logger = LoggerFactory.getLogger(MyClient.class);
- public static void main(String[] args) {
- IoConnector connector = new NioSocketConnector();
- connector.setConnectTimeoutMillis(10 * 1000);
- connector.getFilterChain().addLast("logger", new LoggingFilter());
- connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
- connector.setHandler(new IoHandlerAdapter() {
- @Override
- public void sessionCreated(IoSession session) throws Exception {
- }
- @Override
- public void sessionOpened(IoSession session) throws Exception {
- MyRequestObject myObj = new MyRequestObject("my name", "my value");
- session.write(myObj);
- }
- @Override
- public void sessionClosed(IoSession session) throws Exception {
- }
- @Override
- public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
- }
- @Override
- public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
- logger.error(cause.getMessage(), cause);
- session.close(true);
- }
- @Override
- public void messageReceived(IoSession session, Object message) throws Exception {
- MyResponseObject myResObj = (MyResponseObject) message;
- logger.info("Received " + myResObj);
- session.close(true);
- }
- @Override
- public void messageSent(IoSession session, Object message) throws Exception {
- logger.info("Sent " + message);
- }
- });
- IoSession session = null;
- try {
- ConnectFuture future = connector.connect(new InetSocketAddress("localhost", 10000));
- future.awaitUninterruptibly();
- session = future.getSession();
- } catch (RuntimeIoException e) {
- logger.error(e.getMessage(), e);
- }
- session.getCloseFuture().awaitUninterruptibly();
- connector.dispose();
- }
- }
1. 首先创建I/O Service,这里使用的是NioSocketConnector类来创建了一个IoConnector实例,并设置连接超时为10秒。
2. 创建I/O Filter Chain,和服务器端同样设置了两个IoFilter,一个是LoggingFilter用来记录日志和打印事件消息,另一个是ProtocolCodecFilter实例用来编码数据,这里也使用了ObjectSerializationCodecFactory类来序列化或反序列化数据成java对象。
3. 创建I/O Handler,主要看一下sessionOpened方法,其中在会话建立事件中发送了MyRequestObject对象,然后在messageReceived方法中又接受了MyResponseObject对象。
4. 最后就是IoConnector实例类连接远端的Server。
下面测试一下上面的程序,首先运行MyServer类,然后运行MyClient类,就可以分别在各自的终端上看到事件日志以及发送/接收的对象了。
相关推荐
通过深入学习和实践这个Apache Mina入门Demo,你将掌握如何利用Mina构建网络应用,并了解其核心特性和工作原理,这对于从事Java网络编程或者需要处理大规模并发连接的开发者来说是非常有价值的。
在“Apache Mina入门(完成版)”这份资料中,你将了解到以下关键知识点: 1. **Mina架构**:Mina的核心设计理念是事件驱动和异步通信,它采用了I/O多路复用技术,如Java NIO(非阻塞I/O),以提高服务端处理大量...
Apache Mina是一个开源项目,由Apache软件基金会维护,它是一个网络通信框架,专注于提供高性能、...对于初学者,阅读《Apache Mina入门(完成版)》PDF文档将是一个很好的起点,深入学习Mina的内部机制和实践技巧。
总之,Apache Mina为Java开发者提供了一个强大、灵活的网络通信框架,通过本文档中的"Apache Mina入门(完成版).pdf",你可以系统性地学习如何使用它来构建高效稳定的网络应用。通过实践和理解,你将能够熟练地驾驭...
### Apache Mina 入门详解 #### 一、Mina简介与应用场景 ##### Mina概述 Mina(**Mini Network Application**)是一个简洁且易于使用的Java网络应用开发框架,主要针对TCP/IP通信进行优化设计。它能够帮助开发者...
入门Apache Mina,你需要了解以下几个关键概念: 1. **事件驱动模型**:Mina采用事件驱动模型,当网络事件(如连接建立、数据接收、连接关闭等)发生时,会触发相应的回调方法。 2. **Filter Chain**:通过Filter ...
Apache Mina 框架是一个强大的网络通信应用框架,它主要针对基于TCP/IP和UDP/IP的协议栈,同时也支持Java对象序列化和其他通信方式。Mina 的核心设计目标是帮助开发者快速构建高性能、高可扩展性的网络应用。它采用...
本资源包含两个 pdf 文档,一本根据官方最新文档 (http://mina.apache.org/mina-project/userguide/user-guide-toc.html) 整理的 mina_2.0_user_guide_en.pdf,一个中文翻译的 mina_2.0_user_guide_cn.pdf。...
这就是一个基础的Mina入门实例,展示了如何使用Mina进行客户端-服务器间的通信。随着深入学习,你可以了解更多的Mina特性,如心跳机制、多线程处理、会话管理等,以构建更复杂、高效的网络应用。
了解以上知识点是掌握Apache MINA的基础,通过提供的学习资料,如《Mina2.0学习笔记》、《Apache MINA入门基础》和《Apache MINA Server 2.0中文参考手册》等,可以深入学习MINA的API用法、设计原理和最佳实践。...
### Apache Mina 详解 ...Apache Mina 是一个强大且灵活的网络通信框架,非常适合初学者入门使用。通过本篇文档的学习,希望读者能够对 Mina 的基本原理和使用方法有一个全面的认识,并能够在实际项目中灵活运用。
本入门例子将带你了解如何使用MINA进行基本的网络通信应用开发。 首先,MINA的核心概念包括`Session`、`Filter`和`Handler`。`Session`代表一个网络连接,它是数据传输的通道;`Filter`是处理`Session`中数据的...
3. **Apache MINA 快速入门 lj棒.txt**:这可能是一个快速入门指南,帮助开发者快速理解MINA的基本概念和实现一个简单的网络服务器。 4. **SmartFoxServer 是专门为Adobe Flash设计的跨平台socket服务器**:...
mina是一种高效的互联网网络通信架构,此资料为入门基础。
"示例,我们可以快速入门,了解如何创建一个简单的MINA服务端并处理客户端连接。这个过程涉及到了网络编程的基本概念,如连接建立、数据交换和连接关闭,以及MINA特有的异步事件驱动模型。通过学习和实践这个示例,...
- 通过Apache_Mina入门资料,我们可以学习如何创建服务器和客户端,设置IoFilter和IoHandler,以及如何处理网络事件。 - Apache MINA Server 2.0中文参考手册提供了详细的技术指南,包括API的使用、配置示例和最佳...