码迷,mamicode.com
首页 > Web开发 > 详细

Netty通讯框架样例

时间:2014-07-22 22:45:12      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   java   color   os   

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

建立一个Java工程:

bubuko.com,布布扣

二、编写样例

服务端:

bubuko.com,布布扣
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

客户端:

bubuko.com,布布扣
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

Netty通讯框架样例,布布扣,bubuko.com

Netty通讯框架样例

标签:style   blog   http   java   color   os   

原文地址:http://www.cnblogs.com/super-d2/p/3861092.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!