Client:
PackageCom.server;ImportJava.net.Socket;ImportJava.nio.ByteBuffer; Public classClient { Public Static voidMain (string[] args)throwsException {Socket Socket=NewSocket ("127.0.0.1", 10101); String message= "Hello"; byte[] bytes =message.getbytes (); Bytebuffer Buffer= Bytebuffer.allocate (4 +bytes.length); Buffer.putint (bytes.length);//Netty is Write,bytebuffer is NIO, so use put. buffer.put (bytes); byte[] Array =Buffer.array (); for(inti=0; i<5; i++) {Socket.getoutputstream (). write (array); } socket.close (); }}
Service side:
PackageCom.server;Importjava.net.InetSocketAddress;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;ImportOrg.jboss.netty.bootstrap.ServerBootstrap;ImportOrg.jboss.netty.channel.ChannelPipeline;Importorg.jboss.netty.channel.ChannelPipelineFactory;ImportOrg.jboss.netty.channel.Channels;Importorg.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;ImportOrg.jboss.netty.handler.codec.string.StringDecoder;ImportOrg.jboss.netty.handler.codec.string.StringEncoder; Public classServer { Public Static voidMain (string[] args) {//Service classServerbootstrap bootstrap =NewServerbootstrap (); //boss Thread listener port, worker thread responsible for data read and writeExecutorservice boss =Executors.newcachedthreadpool (); Executorservice worker=Executors.newcachedthreadpool (); //set up the Niosocket factoryBootstrap.setfactory (NewNioserversocketchannelfactory (boss, worker)); //set up a factory for pipelinesBootstrap.setpipelinefactory (Newchannelpipelinefactory () {@Override PublicChannelpipeline Getpipeline ()throwsException {Channelpipeline pipeline=Channels.pipeline (); Pipeline.addlast ("Decoder",NewMydecoder ()); Pipeline.addlast ("Handler1",NewHellohandler ()); returnpipeline; } }); Bootstrap.bind (NewInetsocketaddress (10101)); System.out.println ("Start!!!"); }}
PackageCom.server;ImportOrg.jboss.netty.buffer.ChannelBuffer;ImportOrg.jboss.netty.channel.Channel;ImportOrg.jboss.netty.channel.ChannelHandlerContext;ImportOrg.jboss.netty.handler.codec.frame.FrameDecoder; Public classMydecoderextendsFramedecoder {@Override//the Decode method of Framedecoder protectedObject Decode (Channelhandlercontext CTX, channel channel, Channelbuffer buffer)throwsException {//buffer is Netty's channelbuffer. if(Buffer.readablebytes () > 4) {//must be greater than the basic minimum length of 4 bytes if(Buffer.readablebytes () > 2048) {buffer.skipbytes (Buffer.readablebytes ()); } //MarkBuffer.markreaderindex (); //length intLength =Buffer.readint (); //the remaining data in buffer is less than the length if(Buffer.readablebytes () <length) { //there's a mark on the front, which can be restoredBuffer.resetreaderindex (); //caches the current remaining buffer data, waiting for the remaining packets to arrive return NULL; } //greater than length, start reading data byte[] bytes =New byte[length]; Buffer.readbytes (bytes); //pass the object down to Hellohandler, this time the buffer processing is finished, the back in the buffer when the Framedecoder will help us to loop read, return NewString (bytes); } //cache the currently remaining buffer data, waiting for the rest of the packet to arrive (Framedecoder to help us achieve), return NULL; }}
PackageCom.server;ImportOrg.jboss.netty.channel.ChannelHandlerContext;Importorg.jboss.netty.channel.MessageEvent;ImportOrg.jboss.netty.channel.SimpleChannelHandler; Public classHellohandlerextendsSimplechannelhandler {Private intCount = 1;//no concurrency problem with single thread@Override Public voidMessagereceived (Channelhandlercontext ctx, messageevent e)throwsException {System.out.println (e.getmessage ()+ " " +count); Count++; }}
NETTY10---sub-adhesive bag