Mina Framework----------Android client and server side

Source: Internet
Author: User

1. Related Resources

Mina:http://mina.apache.org/mina-project/quick-start-guide.html

Jar Package Download: http://download.csdn.net/detail/hulan_baby/8424891

Source code (client): http://download.csdn.net/detail/hulan_baby/8424897

Source code (server side): http://download.csdn.net/detail/hulan_baby/8424903

Since I have no resources to divide, so please have the points of the pro people to contribute a little, thank you very much.

2. Engineering Catalogue


Log4j.properties does not need

3. Server-Side Core code



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 {//Inherited 7 methods public static Logger Logger = Logger.getlogger (Timeserverhandler.class);// The server side creates a connection with the client @overridepublic void Sessioncreated (iosession session) {//Displays the client's IP and port System.out.println ( Session.getremoteaddress (). toString ()); Logger.info ("sessioncreated");} Accepts a message from the port, responds to this method to process the message @overridepublic void Messagereceived (iosession session, Object message) throws Exception { String str = message.tostring (), if (Str.trim (). Equalsignorecase ("quit")) {session.close ();//end session return;} else if ( Str.trim (). Equalsignorecase ("Hello")) {Session.write ("How is You!"); /Session.getattribute ("");//Session.setattribute ("");} Date date = new Date (); Session.write (date.tostring ());//Returns the current time of the string System.out.println ("The data received by the client:" + message.tostring ( )); SYSTEM.OUT.PRINTLN ("Message WRItten ... "); @Overridepublic void Exceptioncaught (iosession session, Throwable cause) throws Exception {//TODO auto-generated method s Tublogger.info ("server Exception"); Super.exceptioncaught (session, cause);} This method is called after a message is sent to the client @overridepublic void Messagesent (iosession session, Object message) throws Exception {//TODO Auto-gene Rated Method Stubsuper.messagesent (session, message); Logger.info ("Send success to Client message");} This method is called when the connection to the client is closed @overridepublic void sessionclosed (Iosession session) throws Exception {//TODO auto-generated method Stublogger.info ("server-side shutdown with Client connection"); super.sessionclosed (session);} @Overridepublic void Sessionopened (Iosession session) throws Exception {//TODO auto-generated method Stublogger.info (" Server and Client connection open "); super.sessionopened (session);} @Overridepublic void Sessionidle (iosession session, Idlestatus status) throws Exception {logger.info ("Server enters idle ..."); System.out.println ("IDLE" + session.getidlecount (status));}}
Test:

Cmd-->telnet 127.0.0.1 9123, using this method can also be tested.

There may be errors at this point, "Telnet is not an internal or external command ...".

Workaround: http://jingyan.baidu.com/article/7908e85c6ec355af491ad265.html
4. Client (core code)




5. Results

Start the server side first, and then start the client

Server-side display:

2015-2-5 10:01:43 org.apache.mina.filter.logging.loglevel$4 Log
Information: 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
Information: Opened
[QC] INFO [NioProcessor-1] timeserverhandler.sessionopened (68) | Server and Client Connection open
2015-2-5 10:01:43 org.apache.mina.filter.logging.loglevel$4 Log
Info: received:heapbuffer[pos=0 lim=6 cap=2048:68 6C 6C 6F 0A]
Data received by the client: Hello
Message written ...
[QC] INFO [NioProcessor-1] Timeserverhandler.messagesent (54) | Successfully sent to client message
2015-2-5 10:01:43 org.apache.mina.filter.logging.loglevel$4 Log
Information: sent:heapbuffer[pos=0 lim=14 cap=15:68 6F (6F) [0A]
2015-2-5 10:01:43 org.apache.mina.filter.logging.loglevel$4 Log
Information: sent:heapbuffer[pos=0 lim=0 Cap=0:empty]
2015-2-5 10:01:43 org.apache.mina.filter.logging.loglevel$4 Log
Info: received:heapbuffer[pos=0 lim=5 cap=2048:71 0A]
2015-2-5 10:01:43 org.apache.mina.filter.logging.loglevel$4 Log
Information: Sent:heapbuffer[pos=0 lim=29 cap=30:54 (3 a 30 31 ...]..)
2015-2-5 10:01:43 org.apache.mina.filter.logging.loglevel$4 Log
Information: sent:heapbuffer[pos=0 lim=0 Cap=0:empty]
2015-2-5 10:01:44 org.apache.mina.filter.logging.loglevel$4 Log
Information: CLOSED
[QC] INFO [NioProcessor-1] Timeserverhandler.messagesent (54) | Successfully sent to client message
[QC] INFO [NioProcessor-1] timeserverhandler.sessionclosed (61) | Server-side shutdown connection to the client


Client display:

2015-2-5 10:01:43 org.apache.mina.filter.logging.loglevel$4 Log
Information: CREATED
2015-2-5 10:01:43 org.apache.mina.filter.logging.loglevel$4 Log
Information: Opened
2015-2-5 10:01:43 org.apache.mina.filter.logging.loglevel$4 Log
Info: sent:heapbuffer[pos=0 lim=6 cap=7:68 6C 6C 6F 0A]
2015-2-5 10:01:43 org.apache.mina.filter.logging.loglevel$4 Log
Information: sent:heapbuffer[pos=0 lim=0 Cap=0:empty]
2015-2-5 10:01:43 org.apache.mina.filter.logging.loglevel$4 Log
Info: sent:heapbuffer[pos=0 lim=5 cap=6:71 0A]
2015-2-5 10:01:43 org.apache.mina.filter.logging.loglevel$4 Log
Information: sent:heapbuffer[pos=0 lim=0 Cap=0:empty]
How is YOU!
Thu Feb 10:01:43 CST 2015
2015-2-5 10:01:43 org.apache.mina.filter.logging.loglevel$4 Log
Information: received:heapbuffer[pos=0 lim=14 cap=2048:68 6F (6F) [0A]
2015-2-5 10:01:43 org.apache.mina.filter.logging.loglevel$4 Log
Information: Received:heapbuffer[pos=0 lim=29 cap=2048:54 (3 a 30 31 ...]..)
2015-2-5 10:01:44 org.apache.mina.filter.logging.loglevel$4 Log
Information: CLOSED

Mina Framework----------Android client and server side

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.