Mina small example

Source: Internet
Author: User

This example solves the problem of Chinese garbled characters.

Customer service end:

Mimatimeclient. Java

Package minaexamle. client; import java.net. inetsocketaddress; import Java. NIO. charset. charset; import Java. util. role; import minaexamle. common. baseconfig; import Org. apache. mina. core. future. connectfuture; import Org. apache. mina. core. service. ioconnector; import Org. apache. mina. core. session. iosession; import Org. apache. mina. filter. codec. protocolcodecfilter; import Org. apache. mina. filter. codec. prefixedstrin G. prefixedstringcodecfactory; import Org. apache. mina. filter. logging. loggingfilter; import Org. apache. mina. transport. socket. NIO. niosocketconnector; public class mimatimeclient {public static void main (string [] ARGs) {ioconnector conne= new niosocketconnector (); connector. getfilterchain (). addlast ("logger", new loggingfilter (); connector. getfilterchain (). addlast ("codec", new protocolcodecfilt Er (New prefixedstringcodecfactory (charset. forname ("UTF-8"); connector. sethandler (New timeclienthander (); connectfuture = connector. connect (New inetsocketaddress ("127.0.0.1", baseconfig. port); // wait for the connection to establish connectfuture. awaituninterruptibly (); system. out. println ("connection successful"); iosession session = connectfuture. getsession (); Response SC = new response (system. in); Boolean quit = false; while (! Quit) {string STR = SC. next (); If (Str. equalsignorecase ("quit") {quit = true;} session. write (STR);} // close if (session! = NULL) {If (session. isconnected () {session. getclosefuture (). awaituninterruptibly () ;}connector. Dispose (true );}}}

Timeclienthander. Java

Package minaexamle. client; import Org. apache. mina. core. service. iohandler; import Org. apache. mina. core. session. idlestatus; import Org. apache. mina. core. session. iosession; public class timeclienthander implements iohandler {@ override public void exceptioncaught (iosession arg0, throwable arg1) throws exception {// todo auto-generated method stub arg1.printstacktrace ();} @ override public void messagereceived (iosession arg0, object message) throws exception {// todo auto-generated method stub system. out. println ("client accept information:" + message. tostring () ;}@ override public void messagesent (iosession arg0, object message) throws exception {// todo auto-generated method stub system. out. println ("client sends messages" + message. tostring () ;}@ override public void sessionclosed (iosession session) throws exception {// todo auto-generated method stub system. out. println ("client and:" + session. getremoteaddress (). tostring () + "Disconnect");} @ override public void sessioncreated (iosession session) throws exception {// todo auto-generated method stub system. out. println ("client and:" + session. getremoteaddress (). tostring () + "establish connection");} @ override public void sessionidle (iosession session, idlestatus status) throws exception {// todo auto-generated method stub system. out. println ("idle" + session. getidlecount (Status);} @ override public void sessionopened (iosession arg0) throws exception {// todo auto-generated method stub system. out. println ("open connection ");}}

Server:

 

Minatimeserver. Java

package minaExamle.server;import java.io.IOException;import java.net.InetSocketAddress;import java.nio.charset.Charset;import minaExamle.common.BaseConfig;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.prefixedstring.PrefixedStringCodecFactory;import org.apache.mina.filter.logging.LoggingFilter;import org.apache.mina.transport.socket.nio.NioSocketAcceptor;public class MinaTimeServer {        private static final int PORT= BaseConfig.PORT;        public static void main(String[] args) throws IOException {        IoAcceptor acceptor = new NioSocketAcceptor();        acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );        acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new PrefixedStringCodecFactory(Charset.forName("UTF-8"))));        acceptor.setHandler(  new TimeServerHandler() );                acceptor.getSessionConfig().setReadBufferSize( 2048 );                acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 );                acceptor.bind(new InetSocketAddress(PORT));            }}

Timeserverhandler. Java

Package minaexamle. server; import Java. util. date; import Org. apache. mina. core. service. iohandler; import Org. apache. mina. core. session. idlestatus; import Org. apache. mina. core. session. iosession; public class implements iohandler {@ override public void exceptioncaught (iosession arg0, throwable arg1) throws exception {arg1.printstacktrace () ;}@ override public void handle (iosession session, object message) throws exception {string STR = message. tostring (); system. out. println ("received message:" + Str); If (Str. trim (). equalsignorecase ("quit") {session. close (true); return;} date = new date (); Session. write (date. tostring (); system. out. println ("message written... ") ;}@ override public void messagesent (iosession arg0, object arg1) throws exception {// todo auto-generated method stub system. out. println ("sending message:" + arg1.tostring () ;}@ override public void sessionclosed (iosession session) throws exception {// todo auto-generated method stub system. out. println ("IP:" + session. getremoteaddress (). tostring () + "Disconnect");} @ override public void sessioncreated (iosession session) throws exception {// todo auto-generated method stub system. out. println ("IP:" + session. getremoteaddress (). tostring () ;}@ override public void sessionidle (iosession session, idlestatus status) throws exception {// todo auto-generated method stub system. out. println ("idle" + session. getidlecount (Status);} @ override public void sessionopened (iosession arg0) throws exception {// todo auto-generated method stub }}

Baseconfig. Java

Package minaexamle. Common; public class baseconfig {// server port public static final int Port = 9123 ;}

Server Running

 

Customer Service Running

Mina small example

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.