1, in the absence of any encoder, decoder situation, Netty send and receive data are in accordance with the form of bytebuf, other forms are not legal.
Bytebuf result = (BYTEBUF) msg;byte[] data = new Byte[result.readablebytes ()];result.readbytes (data); String resultstr = new string (data),//receives and prints the client's information System.out.println ("Client said:" + resultstr);//Release resources, This line is critical result.release ();//Send a message to the client string response = "I am ok!"; /In the current scenario, the data sent must be converted to the BYTEBUF array bytebuf encoded = Ctx.alloc (). Buffer (4 * response.length ()); Encoded.writebytes ( Response.getbytes ()); Ctx.write (encoded); Ctx.flush ();
2, receive sends the data operation all is realizes through the handler, handler occupies the very important position in the Netty.
Class Helloserverinhandler extends Channelinboundhandleradapter
3, Netty handler is event-triggered, for example, when client connection server succeeds, the Channelactive method of Helloclientinthandler in client is automatically called.
Receives the server-side message and prints it out @Overridepublic void Channelread (Channelhandlercontext ctx, Object msg) throws Exception { SYSTEM.OUT.PRINTLN ("Client Channelread"); Bytebuf result = (bytebuf) msg; Byte[] data=new byte[result.readablebytes ()];result.readbytes (data); SYSTEM.OUT.PRINTLN ("Server said:" + new String (data)); Result.release ();}
4, the transfer between Channelinboundhandler, by calling Ctx.firechannelread (msg) implementation; Call Ctx.write (msg) is passed to Channeloutboundhandler.
@Overridepublic void Channelread (Channelhandlercontext ctx, Object msg) throws Exception {Log.debug ("InboundHandler1 Channelread "+ctx); Ctx.firechannelread (msg);} @Overridepublic void Channelread (Channelhandlercontext ctx, Object msg) throws Exception {Log.debug ("InboundHandler2 Channelread "+ctx); Bytebuf result= (BYTEBUF) msg;byte[] data=new byte[result.readablebytes ()]; String Res=new string (data); SYSTEM.OUT.PRINTLN ("Client said:" +res); Result.release (); Ctx.write (msg);}
5. After the Ctx.write () method executes, the flush () method needs to be called to make it execute immediately.
@Overridepublic void Write (Channelhandlercontext ctx, Object msg,channelpromise Promise) throws Exception {Log.debug (" OutboundHandler1 write "); String str= "I am OK"; Bytebuf encoded=ctx.alloc (). Buffer (Str.length ()); Encoded.writebytes (Str.getbytes ()); Ctx.write (encoded); Ctx.flush ();}
6, Channeloutboundhandler in the registration time needs to be placed before the last Channelinboundhandler, otherwise it will not be delivered to Channeloutboundhandler.
Channelpipeline Pipeline=ch.pipeline (); Pipeline.addlast (New OutboundHandler1 ());p Ipeline.addlast (New OutboundHandler2 ()); Pipeline.addlast (New InboundHandler1 ());p Ipeline.addlast (New InboundHandler2 ());
7, Encoder, decoder nature is also handler, their execution order, use method and handler consistent.
Execution order is: Encoder first registered after execution, and outboundhandler consistent; decoder is first registered first executed, and Inboundhandler consistent.