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
- 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
[Java]View Plaincopy
- Public class Messageserver {
- public static void Main (String args[]) {
- //Service starter
- Serverbootstrap bootstrap = new Serverbootstrap (new Nioserversocketchannelfactory ( Executors.newcachedthreadpool (), Executors.newcachedthreadpool ()));
- //Set up a class to handle client messages and various message events (Handler)
- Bootstrap.setpipelinefactory (new Channelpipelinefactory () {
- @Override
- Public channelpipeline Getpipeline () throws Exception {
- return Channels.pipeline (new Businesshandler ());
- }
- });
- //Open 8000-Port statement Client Connection
- Bootstrap.bind (new inetsocketaddress (8000));
- }
- private static class Businesshandler extends simplechannelhandler{
- This method is triggered when the server receives a message sent by the client .
- @Override
- public void messagereceived (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
[Java]View Plaincopy
- Public class Messageclient {
- public static void Main (String args[]) {
- Clientbootstrap bootstrap = new Clientbootstrap (new Nioclientsocketchannelfactory ( Executors.newcachedthreadpool (), Executors.newcachedthreadpool ()));
- Bootstrap.setpipelinefactory (new Channelpipelinefactory () {
- @Override
- Public channelpipeline Getpipeline () throws Exception {
- return Channels.pipeline (new Messageclienthandler ());
- }
- });
- Bootstrap.connect (new Inetsocketaddress ("127.0.0.1", 8000));
- }
- private static class Messageclienthandler extends Simplechannelhandler {
- /**
- * When the server is bound to trigger, send messages to the server.
- */
- @Override
- public void channelconnected (Channelhandlercontext ctx, channelstateevent e) {
- //String, constructed into Channelbuffer, passed to the server
- 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
[Java]View Plaincopy
- 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)