Netty通訊架構範例

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   os   

首先到:http://netty.io/ 下載 netty-3.9.2.Final-dist.tar.bz2

建立一個Java工程:

二、編寫範例

服務端:

package netty_test.test;import java.net.InetSocketAddress;import java.util.concurrent.Executors;import org.jboss.netty.bootstrap.ServerBootstrap;import org.jboss.netty.channel.ChannelHandlerContext;import org.jboss.netty.channel.ChannelPipeline;import org.jboss.netty.channel.ChannelPipelineFactory;import org.jboss.netty.channel.ChannelStateEvent;import org.jboss.netty.channel.Channels;import org.jboss.netty.channel.SimpleChannelHandler;import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;public class HelloServer {     public static void main(String args[]) {              // Server服務啟動器              ServerBootstrap bootstrap = new ServerBootstrap(                      new NioServerSocketChannelFactory(                              Executors.newCachedThreadPool(),                              Executors.newCachedThreadPool()));              // 設定一個處理用戶端訊息和各種訊息事件的類(Handler)              bootstrap                      .setPipelineFactory(new ChannelPipelineFactory() {                          @Override                          public ChannelPipeline getPipeline()                                  throws Exception {                              return Channels                                      .pipeline(new HelloServerHandler());                          }                      });              // 開放8000連接埠供用戶端訪問。              bootstrap.bind(new InetSocketAddress(8000));          }                private static class HelloServerHandler extends                  SimpleChannelHandler {                    /**              * 當有用戶端綁定到服務端的時候觸發,列印"Hello world, I‘m server."              *               * @alia OneCoder              * @author lihzh              */              @Override              public void channelConnected(                      ChannelHandlerContext ctx,                      ChannelStateEvent e) {                  System.out.println("Hello world, I‘m server.");              }          }  }
View Code

用戶端:

package netty_test.test;import java.net.InetSocketAddress;import java.util.concurrent.Executors;import org.jboss.netty.bootstrap.ClientBootstrap;import org.jboss.netty.channel.ChannelHandlerContext;import org.jboss.netty.channel.ChannelPipeline;import org.jboss.netty.channel.ChannelPipelineFactory;import org.jboss.netty.channel.ChannelStateEvent;import org.jboss.netty.channel.Channels;import org.jboss.netty.channel.SimpleChannelHandler;import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;public class HelloClient {    public static void main(String args[]) {          // Client服務啟動器          ClientBootstrap bootstrap = new ClientBootstrap(                  new NioClientSocketChannelFactory(                          Executors.newCachedThreadPool(),                          Executors.newCachedThreadPool()));          // 設定一個處理服務端訊息和各種訊息事件的類(Handler)          bootstrap.setPipelineFactory(new ChannelPipelineFactory() {              @Override              public ChannelPipeline getPipeline() throws Exception {                  return Channels.pipeline(new HelloClientHandler());              }          });          // 串連到本地的8000連接埠的服務端          bootstrap.connect(new InetSocketAddress(                  "127.0.0.1", 8000));      }        private static class HelloClientHandler extends SimpleChannelHandler {              /**          * 當綁定到服務端的時候觸發,列印"Hello world, I‘m client."          *           * @alia OneCoder          * @author lihzh          */          @Override          public void channelConnected(ChannelHandlerContext ctx,                  ChannelStateEvent e) {              System.out.println("Hello world, I‘m client.");          }      }  }
View Code

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.