本文共 8565 字,大约阅读时间需要 28 分钟。
,开始进入MINA相关的代码学习,以一个简单HelloWorld程序开始,完成客户端与服务端之间的通讯。
准备工作:
org.apache.mina 版本4.0
org.slf4j 1.6.1 (Simple Logging Facade for Java)mina所使用的日志框架,该版本是与mina4.0匹配的版本。
我是用maven管理项目依赖的,使用apache-maven-3.0.4
IDE:IntelliJ IDEA 10.0.3
开始HelloWorld:
1.maven的pom文件:
4.0.0 example example 1.0 GBK org.apache.mina mina-core 2.0.4 org.slf4j jcl-over-slf4j 1.6.1 org.slf4j slf4j-nop 1.6.1
2. 服务器代码:
a) 首先实现服务端的IoHandler代码SimpleMinaServerHandler。主要功能是打印客户端写入的信息,统计客户端的请求次数以及返回当前客户端是第几次访问。
package com.chiyan.apach.mina.server; import org.apache.mina.core.service.IoHandlerAdapter; import org.apache.mina.core.session.IoSession; import java.util.concurrent.atomic.AtomicInteger; /** * Created by IntelliJ IDEA. * 简单的消息处理器 * User: chiyan * Date: 12-7-19 * Time: 上午12:19 */ public class SimpleMinaServerHandler extends IoHandlerAdapter { private AtomicInteger count = new AtomicInteger(0); /** * 当一个客户端连接进入时 */ @Override public void sessionOpened(IoSession session) throws Exception { System.out.println("client connection : " + session.getRemoteAddress()); } /** * 当一个客户端关闭时 */ @Override public void sessionClosed(IoSession session) throws Exception { System.out.println("client disconnection : " +session.getRemoteAddress() + " is Disconnection"); } /** * 当接收到客户端的信息 * * @param session * @param message * @throws Exception */ @Override public void messageReceived(IoSession session, Object message) throws Exception { String str = (String)message; // 打印客户端 System.out.println("receive client message : [ " + str +" ]"); // 回写消息给客户端 session.write(count.incrementAndGet()); } }
b) 搭建服务端 MainServer:
package com.chiyan.apach.mina.server; 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.codec.textline.TextLineCodecFactory; import org.apache.mina.transport.socket.SocketAcceptor; import org.apache.mina.transport.socket.SocketSessionConfig; import org.apache.mina.transport.socket.nio.NioSocketAcceptor; import java.io.IOException; import java.net.InetSocketAddress; /** * Created by IntelliJ IDEA. * User: chiyan * Date: 12-7-19 * Time: 上午12:27 */ public class MainServer { // 服务器端口 private static final int SERVER_PORT = 8899; public static void main(String[] args) throws Exception { //创建一个非阻塞的Server端Socket,用NIO SocketAcceptor acceptor = new NioSocketAcceptor(); // 定义每次接收数据大小 SocketSessionConfig sessionConfig = acceptor.getSessionConfig(); sessionConfig.setReadBufferSize(2048); //创建接受数据的过滤器 DefaultIoFilterChainBuilder chain = acceptor.getFilterChain(); //设定这个过滤器将一行一行(/r/n)的读取数据 chain.addLast("myChain", new ProtocolCodecFilter(new TextLineCodecFactory())); //设定服务器端的消息处理器: 一个 SimpleMinaServerHandler 对象 acceptor.setHandler(new SimpleMinaServerHandler()); //绑定端口,启动服务器 try { acceptor.bind(new InetSocketAddress(SERVER_PORT)); } catch (IOException e) { e.printStackTrace(); } System.out.println("Mina server is listing port:" + SERVER_PORT); } }
3. 客户端代码
a)首先实现客户端的IoHandler代码SimpleMinaClientHandler。主要功能是向服务端写消息,接收到服务端计数后再回写服务端。
package com.chiyan.apach.mina.client; import org.apache.mina.core.service.IoHandlerAdapter; import org.apache.mina.core.session.IdleStatus; import org.apache.mina.core.session.IoSession; /** * Created by IntelliJ IDEA. * User: chiyan * Date: 12-7-19 * Time: 上午12:31 */ public class SimpleMinaClientHandler extends IoHandlerAdapter { /** * 当客户端接受到消息时 */ @Override public void messageReceived(IoSession session, Object message) throws Exception { Integer num = Integer.valueOf((String)message); if(num == null || num >10){ session.close(true); return; } System.out.println("receive server num : [ " + num + " ]"); Thread.sleep(1000); session.write("client received num is : " + num + ", request next num"); } /** * 当一个客户端被关闭时 */ @Override public void sessionClosed(IoSession session) throws Exception { System.out.println("client disconnect"); } /** * 当一个客户端连接进入时 */ @Override public void sessionOpened(IoSession session) throws Exception { System.out.println("create connection to server :" + session.getRemoteAddress()); session.write("client started"); session.write("Hello World!"); } }
b)搭建客户端MainClient
package com.chiyan.apach.mina.client; import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder; import org.apache.mina.core.future.ConnectFuture; import org.apache.mina.filter.codec.ProtocolCodecFilter; import org.apache.mina.filter.codec.textline.TextLineCodecFactory; import org.apache.mina.transport.socket.nio.NioSocketConnector; import java.net.InetSocketAddress; /** * Created by IntelliJ IDEA. * User: chiyan * Date: 12-7-19 * Time: 上午12:40 */ public class MainClient { /** * 服务器ip */ private static final String SERVER_IP="127.0.0.1"; /** * 服务器端口 */ private static final int SERVER_PORT=8899; public static void main(String []args){ //Create TCP/IP connection NioSocketConnector connector = new NioSocketConnector(); //创建接受数据的过滤器 DefaultIoFilterChainBuilder chain = connector.getFilterChain(); //设定这个过滤器将一行一行(/r/n)的读取数据 chain.addLast("myChain", new ProtocolCodecFilter(new TextLineCodecFactory())); //服务器的消息处理器:一个 SimpleMinaClientHandler 对象 connector.setHandler(new SimpleMinaClientHandler()); //set connect timeout connector.setConnectTimeoutMillis(30 * 1000); //连接到服务器: ConnectFuture cf = connector.connect(new InetSocketAddress(SERVER_IP,SERVER_PORT)); cf.awaitUninterruptibly(); cf.getSession().getCloseFuture().awaitUninterruptibly(); connector.dispose(); } }
运行程序:
1.服务端打印:
2.客户端打印:
转载地址:http://mvyyo.baihongyu.com/