本文主要是介绍【Andorid开发框架学习】之Mina开发之服务器开发,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
上次那篇博客我们讲到了Mina的客户端的开发,如果还有没看过的同学可以看一下,我是传送门。现在,我们来学习一下,Mina的服务器的开发。
一、首先看一下,我的服务器的代码图片:
服务器代码我是在MyEclipse下写的。
二、服务器的整体思路:(同客户端差不多)
-
- 首先,产生一个socket接收对象(SocketAcceptor),用于接收客户端的连接请求;
- 然后,对这个接收器添加我们的I/O过滤器(SSL加密、日志过滤器、编码过滤器等,这里注意,如果添加SSL过滤器,那么一定要第一个添加,否则无法对数据加密);
- 接下来,为连接设置I/O处理器,顾名思义就是处理接收到的消息(这里我们只能设置一个处理器,如果有设置多个,那么默认进入到最后一个I/O处理器中进行处理);
- 最后,将服务器绑定到某端口(如:3456,最好是1024以上,因为1024以下的端口系统占用)。
三、正式编码
这里我同样展示几个比较重要的类来详细说明一下:
1.MinaServer.Javapackage com.mina.example;import java.io.IOException;
import java.net.InetSocketAddress;import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.filter.ssl.SslFilter;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;import com.mina.charset.CharsetFactory;
import com.mina.hanlder.MsgHanler;
import com.mina.ssl.SSLContextGenerator;/*** <pre>* Project Name:MinaServer* Package:com.mina.example* FileName:MinaServer.java* Purpose:服务器* Create Time: 2014-8-19 下午4:59:55* Create Specification:* Modified Time:* Modified by:* Modified Specification:* Version: 1.0* </pre>* * @author myp*/
public class MinaServer {private SocketAcceptor acceptor;public MinaServer() {/** 1.创建一个socket连接,连接到服务器*/acceptor = new NioSocketAcceptor();}public boolean start() {/** 获取过滤器链,用于添加过滤器*/DefaultIoFilterChainBuilder filterChain = acceptor.getFilterChain();/** 2.为连接添加过滤器,SSL、日志、编码过滤器*/// SSLContextGenerator是我们自己写的一个SSL上下文产生器,稍后会讲到SslFilter sslFilter = new SslFilter(new SSLContextGenerator().getSslContext());// a.ssl过滤器,这个一定要第一个添加,否则数据不会进行加密filterChain.addLast("sslFilter", sslFilter);System.out.println("SSL support is added..");// b.添加日志过滤器filterChain.addLast("loger", new LoggingFilter());// c.添加字符的编码过滤器filterChain.addLast("codec", new ProtocolCodecFilter(new CharsetFactory()));/** 3.设置消息处理器,用于处理接收到的消息*/acceptor.setHandler(new MsgHanler());// 设置空闲的时间是30sacceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 30);try {/** 4.将服务器绑定到3456端口*/acceptor.bind(new InetSocketAddress(3456));} catch (IOException e) {e.printStackTrace();return false;}return true;}public static void main(String[] args) {MinaServer server = new MinaServer();server.start();}
}
package com.mina.example;import java.io.IOException;
import java.net.InetSocketAddress;import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.filter.ssl.SslFilter;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;import com.mina.charset.CharsetFactory;
import com.mina.hanlder.MsgHanler;
import com.mina.ssl.SSLContextGenerator;/*** <pre>* Project Name:MinaServer* Package:com.mina.example* FileName:MinaServer.java* Purpose:服务器* Create Time: 2014-8-19 下午4:59:55* Create Specification:* Modified Time:* Modified by:* Modified Specification:* Version: 1.0* </pre>* * @author myp*/
public class MinaServer {private SocketAcceptor acceptor;public MinaServer() {/** 1.创建一个socket连接,连接到服务器*/acceptor = new NioSocketAcceptor();}public boolean start() {/** 获取过滤器链,用于添加过滤器*/DefaultIoFilterChainBuilder filterChain = acceptor.getFilterChain();/** 2.为连接添加过滤器,SSL、日志、编码过滤器*/// SSLContextGenerator是我们自己写的一个SSL上下文产生器,稍后会讲到SslFilter sslFilter = new SslFilter(new SSLContextGenerator().getSslContext());// a.ssl过滤器,这个一定要第一个添加,否则数据不会进行加密filterChain.addLast("sslFilter", sslFilter);System.out.println("SSL support is added..");// b.添加日志过滤器filterChain.addLast("loger", new LoggingFilter());// c.添加字符的编码过滤器filterChain.addLast("codec", new ProtocolCodecFilter(new CharsetFactory()));/** 3.设置消息处理器,用于处理接收到的消息*/acceptor.setHandler(new MsgHanler());// 设置空闲的时间是30sacceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 30);try {/** 4.将服务器绑定到3456端口*/acceptor.bind(new InetSocketAddress(3456));} catch (IOException e) {e.printStackTrace();return false;}return true;}public static void main(String[] args) {MinaServer server = new MinaServer();server.start();}
}
MinaServer就是按照第二步当中的流程走过来的;所以编程的时候最主要的是整体的思路,思路明白了那么编程就会变得异常效率。
2.SSLContextGenerator.Javapackage com.mina.ssl;import java.io.File;
import java.security.KeyStore;import javax.net.ssl.SSLContext;import org.apache.mina.filter.ssl.KeyStoreFactory;
import org.apache.mina.filter.ssl.SslContextFactory;/*** <pre>* Project Name:SSLContextGenerator* Package:com.example.mina.ssl* FileName:SSLContextGenerator.java* Purpose:SSL加密的上下文产生器* Create Time: 2014-8-19 下午4:41:55* Create Specification:* Modified Time:* Modified by:* Modified Specification:* Version: 1.0* </pre>* * @author myp*/
public class SSLContextGenerator {/*** 这个方法,通过keystore和truststore文件返回一个SSLContext对象* * @return*/public SSLContext getSslContext() {SSLContext sslContext = null;try {/** 提供keystore的存放目录,读取keystore的文件内容*/File keyStoreFile = new File("C:/Users/Myp/keystore.jks");/** 提供truststore的存放目录,读取truststore的文件内容*/File trustStoreFile = new File("C:/Users/Myp/truststore.jks");if (keyStoreFile.exists() && trustStoreFile.exists()) {final KeyStoreFactory keyStoreFactory = new KeyStoreFactory();System.out.println("Url is: " + keyStoreFile.getAbsolutePath());keyStoreFactory.setDataFile(keyStoreFile);/** 这个是当初我们使用keytool创建keystore和truststore文件的密码,也是上次让你们一定要记住密码的原因了*/keyStoreFactory.setPassword("123456");final KeyStoreFactory trustStoreFactory = new KeyStoreFactory();trustStoreFactory.setDataFile(trustStoreFile);trustStoreFactory.setPassword("123456");final SslContextFactory sslContextFactory = new SslContextFactory();final KeyStore keyStore = keyStoreFactory.newInstance();sslContextFactory.setKeyManagerFactoryKeyStore(keyStore);final KeyStore trustStore = trustStoreFactory.newInstance();sslContextFactory.setTrustManagerFactoryKeyStore(trustStore);sslContextFactory.setKeyManagerFactoryKeyStorePassword("123456");sslContext = sslContextFactory.newInstance();System.out.println("SSL provider is: "+ sslContext.getProvider());} else {System.out.println("Keystore or Truststore file does not exist");}} catch (Exception ex) {ex.printStackTrace();}return sslContext;}
}
package com.mina.ssl;import java.io.File;
import java.security.KeyStore;import javax.net.ssl.SSLContext;import org.apache.mina.filter.ssl.KeyStoreFactory;
import org.apache.mina.filter.ssl.SslContextFactory;/*** <pre>* Project Name:SSLContextGenerator* Package:com.example.mina.ssl* FileName:SSLContextGenerator.java* Purpose:SSL加密的上下文产生器* Create Time: 2014-8-19 下午4:41:55* Create Specification:* Modified Time:* Modified by:* Modified Specification:* Version: 1.0* </pre>* * @author myp*/
public class SSLContextGenerator {/*** 这个方法,通过keystore和truststore文件返回一个SSLContext对象* * @return*/public SSLContext getSslContext() {SSLContext sslContext = null;try {/** 提供keystore的存放目录,读取keystore的文件内容*/File keyStoreFile = new File("C:/Users/Myp/keystore.jks");/** 提供truststore的存放目录,读取truststore的文件内容*/File trustStoreFile = new File("C:/Users/Myp/truststore.jks");if (keyStoreFile.exists() && trustStoreFile.exists()) {final KeyStoreFactory keyStoreFactory = new KeyStoreFactory();System.out.println("Url is: " + keyStoreFile.getAbsolutePath());keyStoreFactory.setDataFile(keyStoreFile);/** 这个是当初我们使用keytool创建keystore和truststore文件的密码,也是上次让你们一定要记住密码的原因了*/keyStoreFactory.setPassword("123456");final KeyStoreFactory trustStoreFactory = new KeyStoreFactory();trustStoreFactory.setDataFile(trustStoreFile);trustStoreFactory.setPassword("123456");final SslContextFactory sslContextFactory = new SslContextFactory();final KeyStore keyStore = keyStoreFactory.newInstance();sslContextFactory.setKeyManagerFactoryKeyStore(keyStore);final KeyStore trustStore = trustStoreFactory.newInstance();sslContextFactory.setTrustManagerFactoryKeyStore(trustStore);sslContextFactory.setKeyManagerFactoryKeyStorePassword("123456");sslContext = sslContextFactory.newInstance();System.out.println("SSL provider is: "+ sslContext.getProvider());} else {System.out.println("Keystore or Truststore file does not exist");}} catch (Exception ex) {ex.printStackTrace();}return sslContext;}
}
如果不知道如何创建keystore和truststore文件的话,请查看我的这篇博客:http://blog.csdn.net/u010049692/article/details/38686659
3.MsgHandler.Javapackage com.mina.hanlder;import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;/*** <pre>* Project Name:MsgHanler* Package:com.mina.handler* FileName:MsgHanler.java* Purpose:I/O消息处理器,从这里我们就可以看出Mina是事件驱动的* Create Time: 2014-8-19 下午4:55:55* Create Specification:* Modified Time:* Modified by:* Modified Specification:* Version: 1.0* </pre>* * @author myp*/
public class MsgHanler extends IoHandlerAdapter {private static final Logger log = LoggerFactory.getLogger(MsgHanler.class);@Overridepublic void exceptionCaught(IoSession session, Throwable cause)throws Exception {// 出现异常log.error("--------exception--------");super.exceptionCaught(session, cause);}@Overridepublic void messageReceived(IoSession session, Object message)throws Exception {// 从服务器中接收到消息后的处理log.info("--------msg receive--------");log.info("Message:{}", message.toString());super.messageReceived(session, message);}@Overridepublic void messageSent(IoSession session, Object message) throws Exception {// 往服务器中发送消息log.info("Message Send {}", message.toString());super.messageSent(session, message);}@Overridepublic void sessionIdle(IoSession session, IdleStatus status)throws Exception {// session处于空闲的时候log.info("当前连接{}处于空闲状态:{}", session.getRemoteAddress(), status);}@Overridepublic void sessionClosed(IoSession session) throws Exception {// session关闭log.info("Session closed {}->{}", session.getId(),session.getRemoteAddress());super.sessionClosed(session);}
}
package com.mina.hanlder;import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;/*** <pre>* Project Name:MsgHanler* Package:com.mina.handler* FileName:MsgHanler.java* Purpose:I/O消息处理器,从这里我们就可以看出Mina是事件驱动的* Create Time: 2014-8-19 下午4:55:55* Create Specification:* Modified Time:* Modified by:* Modified Specification:* Version: 1.0* </pre>* * @author myp*/
public class MsgHanler extends IoHandlerAdapter {private static final Logger log = LoggerFactory.getLogger(MsgHanler.class);@Overridepublic void exceptionCaught(IoSession session, Throwable cause)throws Exception {// 出现异常log.error("--------exception--------");super.exceptionCaught(session, cause);}@Overridepublic void messageReceived(IoSession session, Object message)throws Exception {// 从服务器中接收到消息后的处理log.info("--------msg receive--------");log.info("Message:{}", message.toString());super.messageReceived(session, message);}@Overridepublic void messageSent(IoSession session, Object message) throws Exception {// 往服务器中发送消息log.info("Message Send {}", message.toString());super.messageSent(session, message);}@Overridepublic void sessionIdle(IoSession session, IdleStatus status)throws Exception {// session处于空闲的时候log.info("当前连接{}处于空闲状态:{}", session.getRemoteAddress(), status);}@Overridepublic void sessionClosed(IoSession session) throws Exception {// session关闭log.info("Session closed {}->{}", session.getId(),session.getRemoteAddress());super.sessionClosed(session);}
}
基本上我们最主要的就是对在I/O处理器这里对收到的消息进行处理,也是编程的核心所在!
四、向服务器发送请求
1.打开CMD命令;
2.telnet 你的IP 端口号(如:telnet 192.168.191.1 3456);
3.然后就可以往服务器输入数据,以换行结束输入(这是因为Mina是以换行来判断输入是否结束的);
注意:如果遇到cmd命令提示无法识别telnet这个命令的话,你可以这样设置:打开控制面板--》程序和功能--》打开或关闭Windows功能--》勾选然后点击确定即可。如下图:
4.然后我们可以看到MyEclipse的控制台输出的信息:可以参考下图(这里我将ssl加密注释了):
五、注意事项
-
- 关于Mina的日志过滤器误区,不知道会不会有同学有这样的认为,我们的log4j-1.2.17.jar就是我们的mina的日志,那么我告诉你你理解错了,log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输出。Mina的日志过滤器是使用了slf4j-log4j12-1.7.6.jar、slf4j-api-1.7.6.jar包;
- log4j的配置问题,如果需要使用Apache的开源项目,我们需要配置log4j.properties文件,下面是他的代码;
log4j.rootCategory=INFO, stdout , R log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n log4j.appender.R=org.apache.log4j.DailyRollingFileAppender log4j.appender.R.File=D\:\\Mina\\logs\\server.log log4j.appender.R.layout=org.apache.log4j.PatternLayout 1log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n log4j.logger.com.neusoft=DEBUG log4j.logger.com.opensymphony.oscache=ERROR log4j.logger.net.sf.navigator=ERROR log4j.logger.org.apache.commons=ERROR log4j.logger.org.apache.struts=WARN log4j.logger.org.displaytag=ERROR log4j.logger.org.springframework=DEBUG log4j.logger.com.ibatis.db=WARN log4j.logger.org.apache.velocity=FATAL log4j.logger.com.canoo.webtest=WARN log4j.logger.org.hibernate.ps.PreparedStatementCache=WARN log4j.logger.org.hibernate=DEBUG log4j.logger.org.logicalcobwebs=WARN
我们可以再这里设置我们的日志输出目录:log4j.appender.R.File=D:\\Mina\\logs\\server.log
3. SSL加密中,如果不知道如何使用keystore生成keystore和truststore文件,可以查看这篇博客:http://blog.csdn.net/u010049692/article/details/38686659
4. 在添加过滤器的时候,处理的顺序是按照添加过滤器的顺序;
5. Mina在使用过滤器的时候,只要在需要的地方添加就可以了,不一定是服务器、客户端都要添加的。就是说,服务器、客户端编程的时候服务器有这个过滤器,客户端可以有也可以没有。
六、Mina服务器源码下载
点我下载
下载后导入到MyEclipse当中,将com.mina.example包下面的MinaServer类中的下面代码注释掉,然后就可以正常运行了!原因是你本地不存在keystore和 truststore文件,如果需要生成请看注意事项中第三条。
1 SslFilter sslFilter = new SslFilter( 2 new SSLContextGenerator().getSslContext()); 3 filterChain.addFirst("sslFilter", sslFilter); 4 System.out.println("SSL support is added..");
那么到这里Mina的开发就暂时告一段落了,如果还有什么需要添加的我以后会在发博客的,欢迎订阅!我的博客园地址:http://www.cnblogs.com/getherBlog/p/3938138.html
这篇关于【Andorid开发框架学习】之Mina开发之服务器开发的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!