Book Recommendation: example code: http://download.csdn.net/detail/jiangtao_st/7677503
w=unionnojs&f=http%3a%2f%2fai.taobao.com%2fauction%2fedetail.htm%3fe%3dnwfw% 252fe17lvwjmraedzvrln4d8gsqrsnltcbl1om% 252br1klltg5xficodxrtutgh9smdpiwxrc30rhswkknj6ztyddmkr6k2rzzwtzgpwlbfzanzymzz1ozjeizwr1bmnhu%26unid%3d28416740 %26ptype%3d100010%26from%3dbasic&k=5ccfdb950740ca16&c=un&b=alimm_0&p=mm_28416740_6610563_ 22760419 ">
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvamlhbmd0yw9fc3q=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
- Netty Server-side implementation
/** * * <p> * Netty Server Simple * </p> * * @author Check Inn HK * @ creation Time: July 7, 2014 * @version: V1.0 */public class Ne Ttyserver {private final int port = 8989; @Testpublic void Nettyserver () {Eventloopgroup bossgroup = new Nioeventloopgroup () ; Eventloopgroup Workergroup = new Nioeventloopgroup (); try {serverbootstrap serverbootstrap = new Serverbootstrap (); Serverbootstrap.group (Bossgroup,workergroup). Channel (nioserversocketchannel.class). Option (Channeloption.so_ BACKLOG, 1024x768). Childhandler (New Childchannelhandler ());//Bind port, synchronous wait channelfuture futrue = serverbootstrap.bind (port) . sync ();//wait for the service listener port to close Futrue.channel (). Closefuture (). sync (); catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} finally{//exit, release thread and other related resources bossgroup.shutdowngracefully (); workergroup.shutdowngracefully ();}} Private class Childchannelhandler extends channelinitializer<socketchannel>{@Overrideprotected void Initchannel (Socketchannel ch) throws Exception {Ch.pipeline (). AddLast (New SimpLeserverhandler ());}}}
- Netty Client Implementation
/** * * <p> * nettyclient Implementation * </p> * * @author Check Inn HK * @ creation Time: July 7, 2014 * @version: V1.0 */public CLA SS Nettyclient {public void connect (int port,string host) {Eventloopgroup group = new Nioeventloopgroup (); try {Bootstrap bo Otstrap = new Bootstrap (); Bootstrap.group (group). Channel (niosocketchannel.class). Option (Channeloption.tcp_nodelay , true). Handler (new channelinitializer<socketchannel> () {@Overrideprotected void Initchannel (Socketchannel ch) Throws Exception {Ch.pipeline (). AddLast (New Simpleclienthandler ());}); /initiates an asynchronous link operation channelfuture channelfuture = Bootstrap.connect (host, port). sync (); Channelfuture.channel (). Closefuture (). Sync ();} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} finally{//off, releasing thread resources group.shutdowngracefully ();}} @Testpublic void Nettyclient () {new Nettyclient (). Connect (8989, "localhost");}}
- Serverhander processing Program
/** * * <p> * Server Receive Message Processing handler * </p> * * @author Check Inn HK * @ creation Time: July 7, 2014 * @version: V1.0 */public cl Simpleserverhandler extends Channelinboundhandleradapter {@Overridepublic void Channelread (Channelhandlercontext CTX, Object msg) throws Exception {bytebuf buf = (bytebuf) msg;byte [] req = new Byte[buf.readablebytes ()];buf.readbytes (re Q); String message = new String (req, "UTF-8"); SYSTEM.OUT.PRINTLN ("netty-server:receive message," + message);}}
- Clienthander Processing program
/** * * <p> * Client Handler * </p> * * @author Check Inn HK * @ creation Time: July 7, 2014 * @version: V1.0 */public class S Impleclienthandler extends Channelinboundhandleradapter {private bytebuf clientmessage;public Simpleclienthandler () { byte [] req = "Call-user-service". GetBytes (); clientmessage = Unpooled.buffer (req.length); Clientmessage.writebytes ( REQ);} @Overridepublic void Channelactive (Channelhandlercontext ctx) throws Exception {Ctx.writeandflush (clientmessage);} @Overridepublic void Channelread (Channelhandlercontext ctx, Object msg) throws Exception {bytebuf buf = (bytebuf) msg;byte [] req = new Byte[buf.readablebytes ()];buf.readbytes (req); String message = new String (req, "UTF-8"); SYSTEM.OUT.PRINTLN ("netty-client:receive message," + message);} @Overridepublic void Exceptioncaught (Channelhandlercontext ctx, Throwable cause) throws Exception {Ctx.close ();}}
Netty instances-Simple service-side-client implementation, gazing at specific