Use the Mina framework to develop the QQ Android client (2) client-server communication

Source: Internet
Author: User

A simple example of a general understanding of the Mina framework, on the basis of the previous section, to see how to implement client-server communication,

Don't say much nonsense, directly look at the code:

public class Test {public  
      
    static void Main (string[] args) throws exception{  
        socketconnector connector = new Nioso Cketconnector ();  
        Iofilter filter = new Protocolcodecfilter (new Textlinecodecfactory ());  
        Connector.getfilterchain (). AddLast ("Vestigge", filter);  
        SocketAddress soketaddress = new Inetsocketaddress ("127.0.0.1", 5469);  
        Connector.sethandler (New ClientHandler ());  
        Connectfuture future= Connector.connect (soketaddress);  
        Future.join ();  
        if (!future.isconnected ()) {  
            System.out.println ("Connection Server failed");  
            return;  
        }  
        Future.getsession (). Write ("Hello");  

You can see the code is very similar to the server-side code, and it is very simple, this is the benefits of the framework, no longer to reinvent the wheel, save a lot of things,

public class ClientHandler extends Iohandleradapter {public  
          
    void messagereceived (iosession arg0, Object message) THR oWS Exception {  
        System.out.println ("Received Server message:" + message.tostring ());  
    }  
      
    public void Exceptioncaught (Iosession arg0, Throwable arg1)  
            throws Exception {  
      
    }  
}

Effect Demo:

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.