1. Client
1.1 write a client
Package COM. boonya. protocol; import java.net. inetsocketaddress; import Java. NIO. charset. charset; import Org. apache. log4j. logger; 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. textline. linedelimiter; import Org. apache. mina. filter. codec. textline. textlinecodecfactory; import Org. apache. mina. transport. socket. NIO. niosocketconnector; import COM. boonya. protocol. handler. request. clientmessagehandler; public class myclient {Private Static logger = logger. getlogger (myclient. class); Private Static string host = "127.0.0.1"; Private Static int Port = 8900; public static void main (string [] ARGs) {// create a non-group Client client ioconnector conne= new niosocketconnector (); // set the connection timeout time to connector. setconnecttimeoutmillis (30000); // Add the filter connector. getfilterchain (). addlast (// Add the message filter "codec ", // The textlinecodec filter provided by Mina based on the text linefeed encoding/decoding. When \ r \ n is displayed, it is deemed that a complete message ends new protocolcodecfilter (New textlinecodecfactory (charset. forname ("UTF-8"), linedelimiter. windows. getvalue (), linedelimiter. windows. getvalue (); // Add the connector of the business logic processor class. sethandler (New clientmessagehandler (); iosession session = NULL; try {connectfuture future = connector ctor. connect (New inetsocketaddress (host, Port); future. awaituninterruptibly (); // wait until the connection is created. Session = Future. getsession (); Session. write ("Mina");} catch (exception e) {logger.info ("client link exception... ");} session. getclosefuture (). awaituninterruptibly (); logger.info ("Mina is closed"); connector. dispose ();}}
1.2 client logic processing
Package COM. boonya. protocol. handler. request; import Org. apache. log4j. logger; import Org. apache. mina. core. service. iohandleradapter; import Org. apache. mina. core. session. iosession; public class clientmessagehandler extends iohandleradapter {Private Static logger = logger. getlogger (clientmessagehandler. class); Public void messagereceived (iosession session, object message) throws exception {string MSG = message. tostring (); logger.info ("the message received by the client is:" + MSG) ;}@ override public void exceptioncaught (iosession session, throwable cause) throws exception {logger.info ("client exception... "+ cause );}}
2. Server
2.1 write Server
Package COM. boonya. protocol; import java.net. inetsocketaddress; import Java. NIO. charset. charset; import Org. apache. log4j. logger; 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. linedelimiter; import Org. apache. mina. filter. codec. textline. textlinecodecfactory; import Org. apache. mina. filter. logging. loggingfilter; import Org. apache. mina. transport. socket. NIO. niosocketacceptor; import COM. boonya. protocol. handler. response. servermessagehandler; public class myserver {Private Static logger = logger. getlogger (myserver. class); Private Static int Port = 8900; public static void main (string [] ARGs) {// create a socket ioacceptor acceptor = NULL for a non-blocking server; try {acceptor = new niosocketacceptor (); // set the LOG filter acceptor. getfilterchain (). addlast ("logger", new loggingfilter (); acceptor. getfilterchain (). addlast (// Add the message filter "codec ", // The textlinecodec filter provided by Mina based on the text linefeed encoding/decoding. When \ r \ n is displayed, it is deemed that a complete message ends new protocolcodecfilter (New textlinecodecfactory (charset. forname ("UTF-8"), linedelimiter. windows. getvalue (), linedelimiter. windows. getvalue (); // sets the size of the buffer for Data Reading. getsessionconfig (). setreadbuffersize (2048); // No operation is performed within 10 s of the read channel to enter the idle acceptor. getsessionconfig (). setidletime (idlestatus. both_idle, 10); // bind the acceptor of the logical processor. sethandler (New servermessagehandler (); // bind the port acceptor. BIND (New inetsocketaddress (port); logger.info ("the server is started successfully .. port: "+ port);} catch (exception e) {e. printstacktrace ();}}
2.2 Server Service Processing
Package COM. boonya. protocol. handler. response; import Java. util. date; import Org. apache. log4j. logger; import Org. apache. mina. core. service. iohandleradapter; import Org. apache. mina. core. session. idlestatus; import Org. apache. mina. core. session. iosession; public class servermessagehandler extends iohandleradapter {public static logger = logger. getlogger (servermessagehandler. class); Public void sessioncreated (iosession session) throws exception {logger.info ("create a connection between the server and the client");} public void sessionopened (iosession session) throws exception {logger.info ("the connection between the server and the client is opened... ");} public void messagereceived (iosession session, Java. lang. object message) throws exception {string MSG = message. tostring (); logger.info ("the data received by the server is:" + MSG); If ("bye ". equals (MSG) {// condition session for server disconnection. close (true);} date = new date (); Session. write (date);} public void messagesent (iosession session, object message) throws exception {logger.info ("the server sent the message successfully... ");} public void sessionidle (iosession session, idlestatus status) throws Java. lang. exception {logger.info ("the server enters idle state... ");} public void exceptioncaught (iosession session, throwable cause) throws exception {logger.info (" the server sends an exception... "+ cause);} public void sessionclosed (iosession session) throws exception {logger.info (" close connection... ");}}