1、相关资源
MINA:http://mina.apache.org/mina-project/quick-start-guide.html
jar包下载:http://download.csdn.net/detail/hulan_baby/8424891
源代码(客户端):http://download.csdn.net/detail/hulan_baby/8424897
源代码(服务器端):http://download.csdn.net/detail/hulan_baby/8424903
由于我没有资源分了,所以请各位有分的亲们贡献一点了,十分感谢。
2、工程目录
log4j.properties不需要
3、服务器端核心代码
import java.util.Date; import org.apache.log4j.Logger; import org.apache.mina.common.IdleStatus; import org.apache.mina.common.IoHandlerAdapter; import org.apache.mina.common.IoSession; public class TimeServerHandler extends IoHandlerAdapter { // 继承了7种方法 public static Logger logger = Logger.getLogger(TimeServerHandler.class); // 服务器端与客户端创建连接 @Override public void sessionCreated(IoSession session) { // 显示客户端的ip和端口 System.out.println(session.getRemoteAddress().toString()); logger.info("sessionCreated"); } // 从端口接受消息,会响应此方法来对消息进行处理 @Override public void messageReceived(IoSession session, Object message) throws Exception { String str = message.toString(); if (str.trim().equalsIgnoreCase("quit")) { session.close();// 结束会话 return; } else if (str.trim().equalsIgnoreCase("hello")) { session.write("how are you !"); // session.getAttribute(""); // session.setAttribute(""); } Date date = new Date(); session.write(date.toString());// 返回当前时间的字符串 System.out.println("客户端收到的数据:" + message.toString()); System.out.println("Message written..."); } @Override public void exceptionCaught(IoSession session, Throwable cause) throws Exception { // TODO Auto-generated method stub logger.info("服务器异常"); super.exceptionCaught(session, cause); } // 向客服端发送消息后会调用此方法 @Override public void messageSent(IoSession session, Object message) throws Exception { // TODO Auto-generated method stub super.messageSent(session, message); logger.info("向客户端消息发送成功"); } // 关闭与客户端的连接时会调用此方法 @Override public void sessionClosed(IoSession session) throws Exception { // TODO Auto-generated method stub logger.info("服务器端关闭与客户端的连接"); super.sessionClosed(session); } @Override public void sessionOpened(IoSession session) throws Exception { // TODO Auto-generated method stub logger.info("服务器与客户端连接打开"); super.sessionOpened(session); } @Override public void sessionIdle(IoSession session, IdleStatus status) throws Exception { logger.info("服务器进入空闲......"); System.out.println("IDLE " + session.getIdleCount(status)); } }测试:
cmd——>telnet 127.0.0.1 9123,运用此方法亦可测试。
此时有可能会有错误,“telnet不是内部或外部命令.......”.
解决方法:http://jingyan.baidu.com/article/7908e85c6ec355af491ad265.html
4、客户端(核心代码)
5、结果
先启动服务器端,再启动客户端
服务器端显示:
2015-2-5 10:01:43 org.apache.mina.filter.logging.LogLevel$4 log
信息: CREATED
/127.0.0.1:61982
[QC] INFO [NioProcessor-1] TimeServerHandler.sessionCreated(17) | sessionCreated
2015-2-5 10:01:43 org.apache.mina.filter.logging.LogLevel$4 log
信息: OPENED
[QC] INFO [NioProcessor-1] TimeServerHandler.sessionOpened(68) | 服务器与客户端连接打开
2015-2-5 10:01:43 org.apache.mina.filter.logging.LogLevel$4 log
信息: RECEIVED: HeapBuffer[pos=0 lim=6 cap=2048: 68 65 6C 6C 6F 0A]
客户端收到的数据:hello
Message written...
[QC] INFO [NioProcessor-1] TimeServerHandler.messageSent(54) | 向客户端消息发送成功
2015-2-5 10:01:43 org.apache.mina.filter.logging.LogLevel$4 log
信息: SENT: HeapBuffer[pos=0 lim=14 cap=15: 68 6F 77 20 61 72 65 20 79 6F 75 20 21 0A]
2015-2-5 10:01:43 org.apache.mina.filter.logging.LogLevel$4 log
信息: SENT: HeapBuffer[pos=0 lim=0 cap=0: empty]
2015-2-5 10:01:43 org.apache.mina.filter.logging.LogLevel$4 log
信息: RECEIVED: HeapBuffer[pos=0 lim=5 cap=2048: 71 75 69 74 0A]
2015-2-5 10:01:43 org.apache.mina.filter.logging.LogLevel$4 log
信息: SENT: HeapBuffer[pos=0 lim=29 cap=30: 54 68 75 20 46 65 62 20 30 35 20 31 30 3A 30 31...]
2015-2-5 10:01:43 org.apache.mina.filter.logging.LogLevel$4 log
信息: SENT: HeapBuffer[pos=0 lim=0 cap=0: empty]
2015-2-5 10:01:44 org.apache.mina.filter.logging.LogLevel$4 log
信息: CLOSED
[QC] INFO [NioProcessor-1] TimeServerHandler.messageSent(54) | 向客户端消息发送成功
[QC] INFO [NioProcessor-1] TimeServerHandler.sessionClosed(61) | 服务器端关闭与客户端的连接
客户端显示:
2015-2-5 10:01:43 org.apache.mina.filter.logging.LogLevel$4 log
信息: CREATED
2015-2-5 10:01:43 org.apache.mina.filter.logging.LogLevel$4 log
信息: OPENED
2015-2-5 10:01:43 org.apache.mina.filter.logging.LogLevel$4 log
信息: SENT: HeapBuffer[pos=0 lim=6 cap=7: 68 65 6C 6C 6F 0A]
2015-2-5 10:01:43 org.apache.mina.filter.logging.LogLevel$4 log
信息: SENT: HeapBuffer[pos=0 lim=0 cap=0: empty]
2015-2-5 10:01:43 org.apache.mina.filter.logging.LogLevel$4 log
信息: SENT: HeapBuffer[pos=0 lim=5 cap=6: 71 75 69 74 0A]
2015-2-5 10:01:43 org.apache.mina.filter.logging.LogLevel$4 log
信息: SENT: HeapBuffer[pos=0 lim=0 cap=0: empty]
how are you !
Thu Feb 05 10:01:43 CST 2015
2015-2-5 10:01:43 org.apache.mina.filter.logging.LogLevel$4 log
信息: RECEIVED: HeapBuffer[pos=0 lim=14 cap=2048: 68 6F 77 20 61 72 65 20 79 6F 75 20 21 0A]
2015-2-5 10:01:43 org.apache.mina.filter.logging.LogLevel$4 log
信息: RECEIVED: HeapBuffer[pos=0 lim=29 cap=2048: 54 68 75 20 46 65 62 20 30 35 20 31 30 3A 30 31...]
2015-2-5 10:01:44 org.apache.mina.filter.logging.LogLevel$4 log
信息: CLOSED
MINA框架----------android客户端与服务器端
原文:http://blog.csdn.net/hulan_baby/article/details/43523977