High concurrency Mina Framework, network Programming (SOCKET) implementation, simple Web chat demo

Source: Internet
Author: User
Tags date now

Service side

Similar to the http://blog.csdn.net/simonchi/article/details/40613923 article

Apache MINA is a Web application framework that helps users to simply develop high-performance and highly scalable Web applications. It provides an event-driven asynchronous API that is abstracted by Java NiO on different transports such as TCP/IP and UDP/IP

A layer of packaging for a Java socket

Apache MINA can often be called:

NIO Framework Library;

A good article on the Mina detailed recommendations: Http://www.iteye.com/topic/1109234#%E6%B3%A8%E4%BA%8C

Client/server framework library;

or a network socket library.

Package Com.tree.demo.mina;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.session.idlestatus;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;public class Minaserver {private static int buf_size = 1024;private static int PORT = 8212;public static void Main (string[] args) {//TODO auto-generated method Stubioacceptor A Cceptor = new Niosocketacceptor (); Acceptor.getfilterchain (). AddLast ("Logger", New Loggingfilter ()); Acceptor.getfilterchain (). AddLast ("Codec", new Protocolcodecfilter (New Textlinecodecfactory (Charset.forname (" UTF-8 "))); Acceptor.sethandler (new Minabizhandler ()); Acceptor.getsessionconfig (). Setreadbuffersize (BUF_SIZE); Acceptor.getsessionconfig (). Setidletime (Idlestatus.both_IDLE, try {acceptor.bind (new inetsocketaddress (PORT)),} catch (IOException e) {//TODO auto-generated catch BLOCKE.PR Intstacktrace ();}}}

The server-side processor overrides the method in the Iohandleradapter class, which handles the data of network interaction, the closing of the session, the processing of the connection, etc.

Package Com.tree.demo.mina;import Java.util.date;import Java.util.scanner;import Org.apache.mina.core.service.iohandleradapter;import Org.apache.mina.core.session.idlestatus;import Org.apache.mina.core.session.iosession;public class Minabizhandler extends Iohandleradapter {public void Exceptioncaught (iosession session, Throwable cause) throws Exception {Cause.printstacktrace ();} @Overridepublic void Sessioncreated (Iosession session) throws Exception {//TODO auto-generated method Stubsuper.sessioncreated (session);} @Overridepublic void Sessionopened (Iosession session) throws Exception {//TODO auto-generated method Stubsuper.sessionopened (session);} @Overridepublic void Sessionclosed (Iosession session) throws Exception {//TODO auto-generated method Stubsuper.sessionclosed (session); Session.close (true);} @Overridepublic void Sessionidle (iosession session, Idlestatus status) throws Exception {//TODO auto-generated method Stu BSystem.out.println ("IDLE:" + session.getidlecount (status) + ">nOW: "+ new Date ());} @Overridepublic void Messagereceived (iosession session, Object message) throws Exception {//TODO auto-generated Method St ubstring str = message.tostring (); System.out.println ("Message Received:" + str); if (Str.trim (). Equalsignorecase ("quit")) {Session.close (true); return;} Scanner sc = new Scanner (system.in); String cnt = Sc.nextline (); StringBuffer say = new StringBuffer (), while (!cnt.equals ("Go")) {say.append ("\ n" + cnt); cnt = Sc.nextline ();} Date now = new Date (); Session.write (say); System.out.println ("Message written!");} @Overridepublic void Messagesent (iosession session, Object message) throws Exception {//TODO auto-generated method Stubsu Per.messagesent (session, message); SYSTEM.OUT.PRINTLN ("Server Sent:" + message.tostring ());} public static void Main (string[] args) {//TODO auto-generated method stub}}

The above implementation of the service side of the program, from the handler program processing, when each received the message from the client, will start to wait for the server input information, know that the input go to the server to send the message to the client, and the client after the input quit, disconnected from the server connection

Client messages


Service-side messages



Through this example, you can play on this basis, write a GUI chat tool is very easy, the core of things have been, the rest is some other processing.






















High concurrency Mina Framework, network Programming (SOCKET) implementation, simple Web chat demo

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.