Receiving and sending strings in the Netty-based message push solution for Android (3)
In the previous article "Concepts and working principles of the Netty-based message push solution for Android (ii)", we introduced some concepts and working principles of Netty, today we will first introduce something called ChannelBuffer.
ChannelBuffer
All messages in Netty must be transmitted in bytes, using ChannelBuffer as the carrier. Simply put, you want to write a string directly. Sorry, an exception is thrown. Although the writer interface parameter defined by Netty is Object, it may also cause misunderstandings to new friends. In the Netty source code, this is the case.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Handler = "brush: java;"> 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 will write a Demo to learn about it.
The server 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 Handler bootstrap class for processing client messages and various message events. setPipelineFactory (new ChannelPipelineFactory () {@ Overridepublic ChannelPipeline getPipeline () throws Exception {return Channels. pipeline (n Ew BusinessHandler () ;}}); // open port 8000 for the client to connect to bootstrap. bind (new InetSocketAddress (8000);} private static class BusinessHandler extends SimpleChannelHandler {// This method is triggered when the server receives a message sent from 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:
Public class MessageClient {public static void main (String args []) {ClientBootstrap bootstrap = new ClientBootstrap (new NioClientSocketChannelFactory (Executors. newCachedThreadPool (), Executors. newCachedThreadPool (); bootstrap. setPipelineFactory (new ChannelPipelineFactory () {@ Overridepublic ChannelPipeline getPipeline () throws Exception {return Channels. pipeline (new MessageClientHandler () ;}}); boot Consumer. connect (new InetSocketAddress ("127.0.0.1", 8000);} private static class MessageClientHandler extends SimpleChannelHandler {/*** sends a message to the server when it is bound to the server. * // @ Overridepublic void channelConnected (ChannelHandlerContext ctx, ChannelStateEvent e) {// construct the String into a ChannelBuffer and pass it 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 and then start the client. You can see that the server prints the following string:
Receive:Hello, I'm client.