Android Netty-based message push scheme string receive and send (iii)

Source: Internet
Author: User

In the previous article, "Android Netty-based message push scheme concept and working principle (ii)", we introduced some of the concept of Netty and working principles of the content, today we first introduce a thing called Channelbuffer.

Channelbuffer


The message passing in the Netty must be passed as a vector in bytes, in the form of Channelbuffer. Simply put, you want to write a string in the past, sorry, throw an exception. Although the interface parameters of the writer defined by Netty are object, this may also be a place where new friends can easily cause misunderstandings. Netty in the source code, this is the judgment.

Sendbuffer Acquire (Object message) {        if (message instanceof Channelbuffer) {            return Acquire ((Channelbuffer) message);        } else if (message instanceof fileregion) {            return Acquire ((fileregion) message);        }         throw new IllegalArgumentException (                "Unsupported message type:" + message.getclass ());    }
Next we write a demo to learn it.

The service-side code is as follows

public class Messageserver {public static void main (String args[]) {//service initiator Serverbootstrap bootstrap = new Serverbootstrap (New Nioserversocketchannelfactory (Executors.newcachedthreadpool (), Executors.newcachedthreadpool ()));// Sets a Class (Handler) bootstrap.setpipelinefactory (new Channelpipelinefactory () {@Overridepublic that handles client messages and various message events Channelpipeline Getpipeline () throws Exception {return channels.pipeline (new Businesshandler ());}}); /Open 8000-Port confession Client Connection Bootstrap.bind (new inetsocketaddress (8000));} This method is triggered when the private static class Businesshandler extends simplechannelhandler{//server receives a message sent by the client @override public void Me Ssagereceived (Channelhandlercontext ctx, messageevent e) throws Exception {Channelbuffer buffer = (channelbuffer) E        . GetMessage ();        System.out.println ("Receive:" +buffer.tostring (Charset.defaultcharset ()));        String msg = buffer.tostring (Charset.defaultcharset ()) + "has been processed!";        Channelbuffer buffer2 = Channelbuffers.buffer (Msg.length ()); Buffer2.wrItebytes (Msg.getbytes ());        E.getchannel (). write (Buffer2); }}}
The client code is as follows

public class Messageclient {public static void main (String args[]) {Clientbootstrap bootstrap = new Clientbootstrap (New Ni Oclientsocketchannelfactory (Executors.newcachedthreadpool (), Executors.newcachedthreadpool ())); Bootstrap.setpipelinefactory (New Channelpipelinefactory () {@Overridepublic channelpipeline getpipeline () throws Exception {return channels.pipeline (new Messageclienthandler ());}); Bootstrap.connect (New Inetsocketaddress ("127.0.0.1", 8000));} private static class Messageclienthandler extends Simplechannelhandler {/** * is triggered when bound to the server, sending messages to the server. */@Overridepublic void channelconnected (Channelhandlercontext ctx, channelstateevent e) {//String, constructed as Channelbuffer, Passed to the server-side string msg = "Hello, I ' m client."; Channelbuffer buffer = Channelbuffers.buffer (Msg.length ()); Buffer.writebytes (Msg.getbytes ()); E.getchannel (). Write (buffer);}}}

Start the server first, then start the client, you can see the server print the following string

Receive:hello, I ' m client.




Android Netty-based message push scheme string receive and send (iii)

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.