Java NIO Framework Netty Tutorial (i) –hello Netty

Source: Internet
Author: User

Let's say that you don't know what Netty is doing or what you can do. That can be a simple search to understand. I can only say that Netty is a NIO framework that can be used to develop distributed Java programs. What you can do in detail, you can make the best use of imagination. Technology is to serve people and not to limit people.

Assuming you have everything, let's start with a piece of code. Program Ape Habits of the first step, nature is "Hello World", just Netty official website sample but abandon "Hello World". Let's write a sample of the simplest "Hello world" to get started.

/** * Netty Server code * * @author LIHZH * @alia onecoder * @blog http://www.coderli.com */public class HelloServer {public St atic void Main (String args[]) {//Server service initiator Serverbootstrap bootstrap = new Serverbootstrap (new Nioserversocketchannelfactory (Executors.newcachedthreadpool (), Executors.newcachedthreadpool ()));// Sets a Class (Handler) bootstrap.setpipelinefactory (new Channelpipelinefactory () {@Overridepublic that handles client messages and various message events Channelpipeline Getpipeline () throws Exception {return channels.pipeline (new Helloserverhandler ());}}); /Open 8000port for client access. Bootstrap.bind (New inetsocketaddress (8000));} private static Class Helloserverhandler Extendssimplechannelhandler {/** * triggers when a client binds to the server, prints "Hello World, I ' m Server. "* * @alia Onecoder * @author lihzh */@Overridepublic void channelconnected (Channelhandlercontext ctx,channelstat Eevent e) {System.out.println ("Hello World, I ' m server.");}}
/** * Netty Client code * * @author LIHZH * @alia onecoder * @blog http://www.coderli.com */public class Helloclient {public static void Main (String args[]) {//Client service initiator Clientbootstrap bootstrap = new Clientbootstrap (New Nioclientsocketchanne Lfactory (Executors.newcachedthreadpool (), Executors.newcachedthreadpool ()));//Set up a class that handles server-side messages and various message events (Handler) Bootstrap.setpipelinefactory (New Channelpipelinefactory () {@Overridepublic channelpipeline getpipeline () throws Exception {return channels.pipeline (new Helloclienthandler ());}); /Connect to local 8000port server Bootstrap.connect (New inetsocketaddress ("127.0.0.1", 8000));}  private static class Helloclienthandler extends Simplechannelhandler {/** * triggers when bound to the server, prints "Hello World, I ' m client." * @alia Onecoder * @author lihzh */@Overridepublic void channelconnected (Channelhandlercontext ctx,channelstateevent e) {S Ystem.out.println ("Hello World, I ' m client.");}}

Since it is distributed, it naturally needs to be divided into multiple services. In Netty, you need to differentiate between server and client services. All clients are bound to the server, and they cannot communicate directly through Netty. (other means of self-use, not included.) )。 Vernacular this communication process, server side open port, for client connection, client initiated the request, connect to server specified port, complete the binding. You can then communicate freely. In fact, is the ordinary socket connection communication process.

Netty Framework is based on the event mechanism, simply said, is what happens, find the relevant processing method. Just follow the fire to find 119, robbed for 1101 reasons. So, here, we're dealing with this event when the client and the server are finished connecting. When the connection was over, Netty knew that he had told me, and I was in charge of dealing with it. This is the role of the framework. Netty, there are a lot of events to be provided, and will be slowly contacted and introduced later.

You should have been able to get started:)

Java NIO Framework Netty Tutorial (i) –hello Netty

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.