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.

[Java]View Plaincopy
  1. Sendbuffer Acquire (Object message) {
  2. if (message instanceof channelbuffer) {
  3. return Acquire ((channelbuffer) message);
  4. } Else if (message instanceof fileregion) {
  5. return Acquire ((fileregion) message);
  6. }
  7. throw New IllegalArgumentException (
  8. "Unsupported message type:" + message.getclass ());
  9. }

Next we write a demo to learn it.

The service-side code is as follows

[Java]View Plaincopy
  1. Public class Messageserver {
  2. public static void Main (String args[]) {
  3. //Service starter
  4. Serverbootstrap bootstrap = new Serverbootstrap (new Nioserversocketchannelfactory (  Executors.newcachedthreadpool (), Executors.newcachedthreadpool ()));
  5. //Set up a class to handle client messages and various message events (Handler)
  6. Bootstrap.setpipelinefactory (new Channelpipelinefactory () {
  7. @Override
  8. Public channelpipeline Getpipeline () throws Exception {
  9. return Channels.pipeline (new Businesshandler ());
  10. }
  11. });
  12. //Open 8000-Port statement Client Connection
  13. Bootstrap.bind (new inetsocketaddress (8000));
  14. }
  15. private static class Businesshandler extends simplechannelhandler{
  16. This method is triggered when the server receives a message sent by the client .
  17. @Override
  18. public void messagereceived (Channelhandlercontext ctx, messageevent e) throws Exception {
  19. Channelbuffer buffer = (channelbuffer) e.getmessage ();
  20. System.out.println ("Receive:" +buffer.tostring (Charset.defaultcharset ()));
  21. String msg = buffer.tostring (Charset.defaultcharset ()) + "has been processed!";
  22. Channelbuffer buffer2 = Channelbuffers.buffer (Msg.length ());
  23. Buffer2.writebytes (Msg.getbytes ());
  24. E.getchannel (). write (Buffer2);
  25. }
  26. }
  27. }

The client code is as follows

[Java]View Plaincopy
  1. Public class Messageclient {
  2. public static void Main (String args[]) {
  3. Clientbootstrap bootstrap = new Clientbootstrap (new Nioclientsocketchannelfactory (  Executors.newcachedthreadpool (), Executors.newcachedthreadpool ()));
  4. Bootstrap.setpipelinefactory (new Channelpipelinefactory () {
  5. @Override
  6. Public channelpipeline Getpipeline () throws Exception {
  7. return Channels.pipeline (new Messageclienthandler ());
  8. }
  9. });
  10. Bootstrap.connect (new Inetsocketaddress ("127.0.0.1", 8000));
  11. }
  12. private static class Messageclienthandler extends Simplechannelhandler {
  13. /** 
  14. * When the server is bound to trigger, send messages to the server.
  15. */
  16. @Override
  17. public void channelconnected (Channelhandlercontext ctx, channelstateevent e) {
  18. //String, constructed into Channelbuffer, passed to the server
  19. String msg = "Hello, I ' m client.";
  20. Channelbuffer buffer = Channelbuffers.buffer (Msg.length ());
  21. Buffer.writebytes (Msg.getbytes ());
  22. E.getchannel (). write (buffer);
  23. }
  24. }
  25. }


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

[Java]View Plaincopy
    1. Receive:hello, I ' m client.


If you are interested, please continue to read the "Android Netty-based message push scheme object Delivery (iv)"

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.