Use Mina to transmit Java objects

Source: Internet
Author: User

This is Apache-Mina-2.0.4, please drink code ....

The following is the object class userinfo. Java to be transmitted.

Package COM. mina. model; import Java. io. serializable;/*** @ see object class transmitted by Mina, requiring it to implement the serializable interface */@ suppresswarnings ("serial") public class userinfo implements serializable {private string name; public userinfo (string name) {This. name = Name ;}public string getname () {return name ;}}

Below is the main server class myserver. Java written by Mina.

Package COM. mina. server; import Java. io. ioexception; import java.net. inetsocketaddress; 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. serialization. objectserializationcodecfactory; import Org. apache. mina. transport. socket. NIO. niosocketacceptor; public class myserver {public static void main (string [] ARGs) throws ioexception {int bindport = 9876; ioacceptor acceptor = new niosocketacceptor (); acceptor. getsessionconfig (). setreadbuffersize (2048); acceptor. getsessionconfig (). setidletime (idlestatus. both_idle, 10); // sets the server's message parsing rule to transmit messages in units of object objects. Note that the object must implement the serializable interface acceptor. getfilterchain (). addlast ("codec", new protocolcodecfilter (New objectserializationcodecfactory (); acceptor. sethandler (New serverhandler (); acceptor. BIND (New inetsocketaddress (bindport); system. out. println ("minaserver is startup, and it's listing on: =" + bindport );}}

Below is the server's message processor serverhandler. Java

Package COM. mina. server; import Org. apache. mina. core. service. iohandleradapter; import Org. apache. mina. core. session. iosession; import COM. mina. model. userinfo; public class serverhandler extends iohandleradapter {@ overridepublic void messagereceived (iosession session, object message) throws exception {userinfo UI = (userinfo) message; // We have set the Server Message Parsing rule to transmit the system in the unit of userinfo object. out. println ("user name sent from the client:" + UI. getname (); Session. write (New userinfo (UI. getname () + "==>> is a mysterious person") ;}@ overridepublic void sessionopened (iosession session) throws exception {system. out. println ("incoming client:" + session. getremoteaddress ());}}

Next, the main client class myclient. Java compiled by Mina

Package COM. mina. client; import java.net. inetsocketaddress; import Org. apache. mina. core. service. ioconnector; import Org. apache. mina. filter. codec. protocolcodecfilter; import Org. apache. mina. filter. codec. serialization. objectserializationcodecfactory; import Org. apache. mina. transport. socket. NIO. niosocketconnector; import COM. mina. model. userinfo; public class myclient {public static void main (string [] ARGs) {ioconnector conne= new niosocketconnector (); connector. setconnecttimeoutmillis (30000); connector. getfilterchain (). addlast ("codec", new protocolcodecfilter (New objectserializationcodecfactory (); connector. sethandler (New clienthandler (New userinfo ("Zhang qiling"); connector. connect (New inetsocketaddress ("127.0.0.1", 9876 ));}}

Finally, the client's message processor clienthandler. Java

Package COM. mina. client; import Org. apache. mina. core. service. iohandleradapter; import Org. apache. mina. core. session. iosession; import COM. mina. model. userinfo; public class clienthandler extends iohandleradapter {private final userinfo UI; Public clienthandler (userinfo UI) {This. ui = UI;} @ overridepublic void sessionopened (iosession session) throws exception {session. write (UI) ;}@ overridepublic void messagereceived (iosession session, object message) throws exception {userinfo UI = (userinfo) message; system. out. println ("user name sent from the server:" + UI. getname () ;}@ overridepublic void exceptioncaught (iosession session, throwable cause) throws exception {system. out. println ("and" + session. getremoteaddress () + "error occurred during communication: [" + cause. getmessage () + "]... the connection will be closed .... "); Session. close (false); Session. getservice (). dispose ();}}
Related Article

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.