Java socket multithreaded communication uses Mina as the server

Source: Internet
Author: User

The client code is not changed, refer to http://www.cnblogs.com/Westfalen/p/6251473.html

The service-side code is as follows:

Importjava.io.IOException;Importjava.net.InetSocketAddress;ImportOrg.apache.mina.core.service.IoHandlerAdapter;ImportOrg.apache.mina.core.session.IdleStatus;Importorg.apache.mina.core.session.IoSession;ImportOrg.apache.mina.filter.codec.ProtocolCodecFilter;Importorg.apache.mina.filter.codec.textline.TextLineCodecFactory;ImportOrg.apache.mina.transport.socket.nio.NioSocketAcceptor;/*** Using Mina as the server for socket communication * requires JAR package: Mina-core-2.0.16.jar, Slf4j-api-1.7.21.jar * Jar Package:http://mina.apache.org/downloads-mina.html  * */ Public classMinaserver { Public Static voidMain (string[] args) {Try {            //4-Step Operation//1. New Niosocketacceptor Case ObjectNiosocketacceptor acceptor =NewNiosocketacceptor (); //2. Set the message Processing objectAcceptor.sethandler (NewMyserverhandler ()); //3. Set the message decoderAcceptor.getfilterchain (). AddLast ("Codec",NewProtocolcodecfilter (Newtextlinecodecfactory ())); //acceptor.getsessionconfig (). Setidletime (Idlestatus.both_idle, 3); // //Set Idle Duration//4. Bind Port Open ServiceAcceptor.bind (NewInetsocketaddress (9999)); } Catch(IOException e) {e.printstacktrace (); }    }}/*** Classes that specialize in message processing, enabling decoupling of network connections and message processing*/classMyserverhandlerextendsIohandleradapter {/**handling of abnormal times*/@Override Public voidExceptioncaught (iosession session, Throwable Cause)throwsException {System.out.println ("Exceptioncaught"); }    /**Receive Message*/@Override Public voidMessagereceived (iosession session, Object message)throwsException {System.out.println ("Messagereceived" +message); //Session.write ("reply" + message);//Send the message back to the client as soon as you receive the message    }    /**Send Message*/@Override Public voidMessagesent (iosession session, Object message)throwsException {System.out.println ("Messagesent" +message); }    /**Handling when communication is idle*/@Override Public voidSessionidle (iosession session, Idlestatus status)throwsException {System.out.println ("Sessionidle"); }}

Java socket multithreaded communication uses Mina as the server

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.