Client:
Packagecom.client;Importjava.net.InetSocketAddress;ImportJava.util.Scanner;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;ImportOrg.jboss.netty.bootstrap.ClientBootstrap;ImportOrg.jboss.netty.channel.Channel;Importorg.jboss.netty.channel.ChannelFuture;ImportOrg.jboss.netty.channel.ChannelPipeline;Importorg.jboss.netty.channel.ChannelPipelineFactory;ImportOrg.jboss.netty.channel.Channels;Importorg.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;ImportOrg.jboss.netty.handler.codec.string.StringDecoder;ImportOrg.jboss.netty.handler.codec.string.StringEncoder;/*** Netty Client entry*/ Public classClient { Public Static voidMain (string[] args) {//Service classClientbootstrap bootstrap =NewClientbootstrap (); //2 Thread PoolsExecutorservice boss =Executors.newcachedthreadpool (); Executorservice worker=Executors.newcachedthreadpool (); //set up a niosocket factory, pass in 2 thread pools,Bootstrap.setfactory (NewNioclientsocketchannelfactory (boss, worker)); //set up a piping factoryBootstrap.setpipelinefactory (Newchannelpipelinefactory () {//The factory returns to the Channelpipeline, is the pipeline, the pipeline contains the filter,@Override PublicChannelpipeline Getpipeline ()throwsException {Channelpipeline pipeline=Channels.pipeline (); //It's a byte stream, not a character.Pipeline.addlast ("Decoder",NewStringdecoder ()); Pipeline.addlast ("Encoder",NewStringencoder ()); Pipeline.addlast ("Hihandler",NewClientHandler ()); returnpipeline; } }); //connecting to the service sideChannelfuture connect = Bootstrap.connect (NewInetsocketaddress ("127.0.0.1", 10101)); Channel Channel=Connect.getchannel (); System.out.println ("Client Start"); Scanner Scanner=NewScanner (system.in); while(true) {System.out.println ("Please enter"); Channel.write (Scanner.next ()); } }}
Packagecom.client;ImportOrg.jboss.netty.channel.ChannelHandlerContext;Importorg.jboss.netty.channel.ChannelStateEvent;Importorg.jboss.netty.channel.ExceptionEvent;Importorg.jboss.netty.channel.MessageEvent;ImportOrg.jboss.netty.channel.SimpleChannelHandler;/*** Message Acceptance processing class*/ Public classClientHandlerextendsSimplechannelhandler {/*** Receive Messages*/@Override Public voidMessagereceived (Channelhandlercontext ctx, messageevent e)throwsException {String s=(String) e.getmessage (); System.out.println (s); Super. messagereceived (CTX, E); } /*** Catch Exceptions*/@Override Public voidExceptioncaught (Channelhandlercontext ctx, exceptionevent e)throwsException {System.out.println ("Exceptioncaught"); Super. Exceptioncaught (CTX, E); } /*** New Connection*/@Override Public voidChannelconnected (Channelhandlercontext ctx, channelstateevent e)throwsException {System.out.println ("Channelconnected"); Super. channelconnected (CTX, E); } /*** Must be link has been established, when the channel is closed to trigger. The service side is also called off. */@Override Public voidChanneldisconnected (Channelhandlercontext ctx, channelstateevent e)throwsException {System.out.println ("Channeldisconnected"); Super. channeldisconnected (CTX, E); } /*** The channel is triggered when it is closed and is also called when the server is closed. */@Override Public voidChannelclosed (Channelhandlercontext ctx, channelstateevent e)throwsException {System.out.println ("Channelclosed"); Super. channelclosed (CTX, E); }}
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;/*** Get started with Netty server*//*Netty version of the approximate version of netty3.x and netty4.x, Netty5.xnetty can be used in the field: 1 Distributed Process communications Hadoop, Dubbo, Akka and other distributed functions of the framework, as long as the Java, The underlying RPC communication is based on Netty implementation, these frameworks use the version is usually still in the NETTY3.X2, Game Server Development page tour is Java, the server is Netty or Mina, the latest game server Some companies may have started to adopt netty4.x or netty5.x*/ 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 factory, the boss and worker threads will be assigned a Selector,boss selector responsible for listening ports, and the worker's selector is responsible for reading and writing tasks. Bootstrap.setfactory (NewNioserversocketchannelfactory (boss, worker)); //set up a factory for pipelinesBootstrap.setpipelinefactory (Newchannelpipelinefactory () {//the pipes are filled with filters,@Override PublicChannelpipeline Getpipeline ()throwsException {Channelpipeline pipeline=Channels.pipeline (); //The pass is not a character, it is a byte stream. //Stringencoder/stringdecoder doesn't have to turn Channelbuffer into a string where the message is received. Pipeline.addlast ("Decoder",NewStringdecoder ());//Stringdecoder is the upstream pipeline. Pipeline.addlast ("Encoder",NewStringencoder ());//Stringencoder is the downstream pipeline. Pipeline.addlast ("Hellohandler",NewServerhandler ());//Serverhandler are upstream and downstream pipelines. returnpipeline; } }); Bootstrap.bind (NewInetsocketaddress (10101));//receive messages in HellohandlerSYSTEM.OUT.PRINTLN ("Start!!!"); }}
PackageCom.server;ImportOrg.jboss.netty.buffer.ChannelBuffer;ImportOrg.jboss.netty.channel.ChannelHandlerContext;Importorg.jboss.netty.channel.ChannelStateEvent;Importorg.jboss.netty.channel.ExceptionEvent;Importorg.jboss.netty.channel.MessageEvent;ImportOrg.jboss.netty.channel.SimpleChannelHandler;/*** Message Acceptance processing class*/ Public classServerhandlerextendsSimplechannelhandler {/*** Receive Messages*/@Override Public voidMessagereceived (Channelhandlercontext ctx, messageevent e)throwsException {String s=(String) e.getmessage (); /*Channelbuffer message = (Channelbuffer) e.getmessage (); string s = new string (Message.array ());*/System.out.println (s); //Write back DataCtx.getchannel (). Write ("Hi"); Super. messagereceived (CTX, E); } /*** Catch Exceptions*/@Override Public voidExceptioncaught (Channelhandlercontext ctx, exceptionevent e)throwsException {System.out.println ("Exceptioncaught"); Super. Exceptioncaught (CTX, E); } /*** New connection, commonly used to detect if IP is blacklisted*/@Override Public voidChannelconnected (Channelhandlercontext ctx, channelstateevent e)throwsException {System.out.println ("Channelconnected"); Super. channelconnected (CTX, E); } /*** Must be the link has been established successfully, close the channel when the trigger, first trigger channeldisconnected in the trigger channelclosed. * * Can be disconnected when users know the user's cache data, etc.*/@Override Public voidChanneldisconnected (Channelhandlercontext ctx, channelstateevent e)throwsException {System.out.println ("Channeldisconnected"); Super. channeldisconnected (CTX, E); } /*** Even if there is no success, when the channel is closed trigger * * Can be disconnected when users know the user's cache data, etc.*/@Override Public voidChannelclosed (Channelhandlercontext ctx, channelstateevent e)throwsException {System.out.println ("Channelclosed"); Super. channelclosed (CTX, E); }}
Netty2---server and client