Bo Master once to Netty4 Helloword very interested, also simply wrote a small chat room Java code, now look again, browse this cow People's blog Click to see
I find it rewarding, so I share it.
This is the use of Netty on Android as a client, to communicate with the service side of the small case, pure HelloWorld, the effect is to send a message to the server in the Android program, and then the service side also back a message to the client, very simple demo,. Daniel, don't throw up the groove!
1.demo structure
2. Service-Side code:
Server.java
Package Org.jan.netty.demo;import Io.netty.bootstrap.serverbootstrap;import Io.netty.channel.channelfuture;import Io.netty.channel.eventloopgroup;import Io.netty.channel.nio.nioeventloopgroup;import Io.netty.channel.socket.nio.nioserversocketchannel;public class Myhelloserver {private static final int PORT = 7878; public static void Main (string[] args) {Eventloopgroup parentgroup = new Nioeventloopgroup (); Eventloopgroup Childrengroup = new Nioeventloopgroup (); try {serverbootstrap serverbootstrap = new Serverbootstrap (); Serverbootstrap.group (Parentgroup, Childrengroup); Serverbootstrap.channel (nioserversocketchannel.class);// Add worker thread Serverbootstrap.childhandler (new Myhelloserverinitializer ());//server bound port listener Channelfuture CF = Serverbootstrap.bind (PORT). Sync ()//listener server off Listen Cf.channel (). Closefuture (). sync (); catch (Exception e) {e.printstacktrace ();} finally {parentgroup.shutdowngracefully (); Childrengroup.shutdowngracefully ();}}}
Myhelloserverhandler.java
package Org.jan.netty.demo;import Io.netty.channel.channelhandlercontext;import Io.netty.channel.simplechannelinboundhandler;import Java.net.inetaddress;import Java.nio.charset.Charset;public Class Myhelloserverhanler extends simplechannelinboundhandler<string> {@Overrideprotected void channelRead0 ( Channelhandlercontext ctx, String msg) throws Exception {string recstr = new String (Msg.getbytes (), Charset.forname (" UTF-8 "));//Receive Message print output System.out.println (Ctx.channel (). Remoteaddress () +" say: "+recstr);//Return to client Ctx.writeandflush ( "Received your message!\n");} @Overridepublic void Channelactive (Channelhandlercontext ctx) throws Exception {System.out.println ("ramoteaddress:" + Ctx.channel (). Remoteaddress () + "active!"); /ctx.writeandflush ("Welcome to" + inetaddress.getlocalhost (). GetHostName () + "' s service!\n"); Ctx.writeandflush (" I am the service side, I am the service side! \ n "); super.channelactive (CTX);}}
3.android Client code:
Package Org.jan.nio.exapmle;import Io.netty.bootstrap.bootstrap;import Io.netty.channel.channel;import Io.netty.channel.channelhandlercontext;import Io.netty.channel.channelinboundhandleradapter;import Io.netty.channel.nio.nioeventloopgroup;import Io.netty.channel.socket.nio.niosocketchannel;import Java.net.inetsocketaddress;import Android.app.activity;import Android.content.context;import Android.os.Bundle; Import Android.util.log;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.toast;public class Clientactivity extends Activity {private static final String TAG = "Mainactivity";p ublic static final String HOST = "192.168.50.110";p ublic static int PORT = 7878;private Nioev Entloopgroup group;private Button sendbutton;private static context context; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); context = this; Sendbutton = (Button) findvIewbyid (R.id.netty_send_button); Sendbutton.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick ( View v) {connected ();}});} Connection to socket server private void connected () {new Thread () {@Overridepublic void Run () {group = new nioeventloopgroup (); try {/ /Client Service Launcher 3.x clientbootstrap//changed to Bootstrap, and the constructor changed very much, here with no parameter constructs. Bootstrap Bootstrap = new Bootstrap ();//Specify channel type Bootstrap.channel (Niosocketchannel.class);// Specify Handlerbootstrap.handler (new Myclientinitializer);//Specify Eventloopgroupbootstrap.group (group);// The service-side channel Channel = Bootstrap.connect (new inetsocketaddress (Host,port)) connected to the 8000 port of the destination IP. Sync (). Channel (); Channel.writeandflush ("I am the client, I am the client!") \ r \ n "); Channel.read ();} catch (Interruptedexception e) {e.printstacktrace ();}}}. Start ();} @Overrideprotected void OnDestroy () {Super.ondestroy (); if (group!=null) {group.shutdowngracefully ();}}}
4. Achieve the effect:
To download the demo
[Android Beginner]android and Netty4 first experience