Java NIO Framework Netty Course (i) –hello Netty

Source: Internet
Author: User

First verbose 2. If you don't know what to do with Netty. It can be a simple search to find out. I can only say that Netty is NIO. The framework, which can be used to develop distributed Java plans. Details can be done and we can try to use your imagination.

Technology. It is to serve people, not the limitations of human life.

With everything, let's start with a piece of code.

Program apes are accustomed to the first step, nature is "Hello world". Just Netty official website sample but abandoned the "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 {/** * is triggered when binding to the server. Print "Hello world, I ' m client." * * @alia Onecoder * @author lihzh */@Overridepublic void channelconnected (Channelhandlercon Text Ctx,channelstateevent e) {System.out.println ("Hello World, I ' m client.");}}

Since it's distributed. Naturally, multiple services are divided.

In Netty, you need to differentiate between server and client services.

All of the client is bound to the server. There is no direct communication between them through the 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.

The Netty framework is based on the event mechanism, briefly. What's wrong. In order to find the relevant processing method. Find a fire 119 and find 1101 reasons to be robbed.

So, here. What we are dealing with is when the client connects to the server after the event is completed.

When the connection was completed, Netty knew that he told me that I was in charge of the deal. This is the role of the framework. Netty, there are many activities provided, after the contact will be introduced slowly.

You should have been able to get started:)

Java NIO Framework Netty Course (i) –hello Netty

Related Article

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.