Use Apache Mina

Source: Internet
Author: User
Copyright:Original works can be reprinted. During reprinting, you must mark the original source, author information, and this statement in hyperlink form. Otherwise, legal liability will be held. Http://java999.blog.51cto.com/259217/148287
<>

Description of the basic class of Mina in Apache Mina ioaccepter is equivalent to the ioconnector on the server side in the network application program. It is equivalent to the iosession on the client. A connection instance from the current client to the server, iohandler, business processing logic, iofilter, is used for communication interface with business layer server: minaserver. java
Public class minaserver {
Private Static final int server_port = 9990;

Public static void main (string [] ARGs) throws throwable {
// Server entry
Ioacceptor acceptor = new socketacceptor ();
Socketacceptorconfig CFG = new socketacceptorconfig ();
Cfg. setreuseaddress (true );
// Set this filter to read data in units of Objects
Cfg. getfilterchain (). addlast ("codec ",
New protocolcodecfilter (New objectserializationcodecfactory ()));
// Add a log Filter
Cfg. getfilterchain (). addlast ("logger", new loggingfilter ());
// Receives the access connection and the iohandler trigger event
Acceptor. BIND (New inetsocketaddress (server_port), new serverhandler (), CFG );
System. Out. println ("the server listening on port" + server_port );
}
}

Server-side service processing:
Public class serverhandler extends iohandleradapter
{
/**
* Triggered when a new connection exists.
*/
Public void sessionopened (iosession session ){
System. Out. println ("sessionopened ");
}
/**
* Receive a message from the client
*/
Public void messagereceived (iosession session, object message ){
System. Out. println ("server messagereceived! ");
Citymessage CM = (citymessage) message;
System. Out. println (CM. getcode () + ":" + cm. getname ());
// Send messages to the client
Session. Write (CM );
}
/**
* Triggered when an exception occurs.
*/
Public void exceptioncaught (iosession session, throwable cause ){
Session. Close ();
}
/**
* Triggered when the connection is closed
*/
Public void sessionclosed (iosession SSN) throws exception {
System. Out. println ("session closed from" + SSN. getremoteaddress ());
}
}

Message object to be sent:
Public class citymessage implements serializable {
Private int code;
Private string name;
Public int getcode (){
Return code;
}
Public void setcode (INT code ){
This. Code = code;
}
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
}

Client:
Public class minaclient {
Private Static final string hostname = "localhost ";
Private Static final int Port = 9990;

Public static void main (string [] ARGs) throws throwable {
// Connector is the client Portal
Socketconnector conne= new socketconnector ();
Socketconnectorconfig CFG = new socketconnectorconfig ();
// Set this filter to read data in units of Objects
Cfg. getfilterchain (). addlast ("codec ",
New protocolcodecfilter (New objectserializationcodecfactory ()));
// Add a log Filter
Cfg. getfilterchain (). addlast ("logger", new loggingfilter ());
// Initiate a connection to the remote server and trigger the event to iohandler
Connector. Connect (New inetsocketaddress (hostname, port ),
New clienthandler (), CFG );
}
}

Client Service Processing:
Public class clienthandler extends iohandleradapter {
Public clienthandler (){
Super ();
}
/**
* Triggered when a new connection exists.
*/
Public void sessionopened (iosession session ){
// Create a citymessage for sending
Citymessage CM = new citymessage ();
Cm. setcode (547600 );
Cm. setname ("fengshan ");
Session. Write (CM );
}
/**
* Receive a message from the client
*/
Public void messagereceived (iosession session, object message ){
System. Out. println ("client messagereceived! ");
Citymessage CM = (citymessage) message;
System. Out. println (CM. getcode () + ":" + cm. getname ());
}
/**
* Triggered when an exception occurs.
*/
Public void exceptioncaught (iosession session, throwable cause ){
Session. Close ();
}
/**
* Triggered when the connection is closed
*/
Public void sessionclosed (iosession SSN) throws exception {
System. Out. println ("session closed from" + SSN. getremoteaddress ());
}
} To develop a mina application, simply put, create links, set filter rules, and write your own message processor! Filter: objectserializationcodecfactory: sets this filter to read data in units of objects. Textlinecodecfactory: sets this filter to read data from one row (/R/n). Note: (Excerpted from the internet) the socket established by Apache Mina is TCP or UDP, when the filter does not add the codec option, the message object in the public void messagereceived (iosession session, object message) {} function of the handler class derived from iohandleradapter is an iobuffer instance, as follows: @ override
Public void messagereceived (iosession session, object message)
Throws exception {

If (! (Message instanceof iobuffer )){
Return;
}

Iobuffer RB = (iobuffer) message;
Callback. messagereceived (Java. NiO. bytebuffer. Wrap (RB. Array (), 0, RB. Buf (). Limit ()));
}

If you use Mina to create a socket, add the following:
Iofilter codec_filter = new protocolcodecfilter (
New textlinecodecfactory ());
Connector (acceptor). getfilterchain (). addlast ("codec", codec_filter );
If the socket for sending data is not set the same or is not a socket created using Mina, you need to add '/N' at the end of the data to send the data to indicate that the string ends.

This article from the "Qingshan" blog, please be sure to keep this source http://java999.blog.51cto.com/259217/148287

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.