zoukankan      html  css  js  c++  java
  • zbb20180930 java-io-nio-netty 代码例子

    maven
    <dependency>
                <groupId>io.netty</groupId>
                <artifactId>netty</artifactId>
                <version>3.3.0.Final</version>
            </dependency>
     
    TestNettyServer
     
    package com.zbb.test.netty;
     
    import java.net.InetSocketAddress;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
     
    import org.jboss.netty.bootstrap.ServerBootstrap;
    import org.jboss.netty.channel.Channel;
    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.ExceptionEvent;
    import org.jboss.netty.channel.MessageEvent;
    import org.jboss.netty.channel.SimpleChannelHandler;
    import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
    import org.jboss.netty.handler.codec.string.StringDecoder;
    import org.jboss.netty.handler.codec.string.StringEncoder;
     
    public class TestNettyServer {
     
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            // 创建netty server
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            // 创建 两个线程池
            ExecutorService pool1 = Executors.newCachedThreadPool();
            ExecutorService pool2 = Executors.newCachedThreadPool();
            // 创建线程工程
            serverBootstrap.setFactory(new NioServerSocketChannelFactory(pool1, pool2));
            // 创建管道工厂
            serverBootstrap.setPipelineFactory(new ChannelPipelineFactory() {
     
                public ChannelPipeline getPipeline() throws Exception {
                    ChannelPipeline pipeline = org.jboss.netty.channel.Channels.pipeline();
                    pipeline.addLast("encode", new StringEncoder());
                    pipeline.addLast("decode", new StringDecoder());
                    pipeline.addLast("serverHandler", new ZbbNettyHandlerServer());
     
                    // TODO Auto-generated method stub
                    return pipeline;
                }
            });
            //绑定端口
            Channel bind = serverBootstrap.bind(new InetSocketAddress(8080));
            System.out.println("server start ...");
        }
     
    }
     
    class ZbbNettyHandlerServer extends SimpleChannelHandler {
     
        @Override
        public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
            // TODO Auto-generated method stub
            super.channelClosed(ctx, e);
            System.out.println("channelClosed");
        }
     
        @Override
        public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
            // TODO Auto-generated method stub
            super.channelDisconnected(ctx, e);
            System.out.println("channelDisconnected");
        }
     
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
            // TODO Auto-generated method stub
            super.exceptionCaught(ctx, e);
            System.out.println("exceptionCaught");
        }
     
        @Override
        public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
            // TODO Auto-generated method stub
            super.messageReceived(ctx, e);
            System.out.println("messageReceived");
            System.out.println("server received client message:" + e.getMessage());
            Channel channel = ctx.getChannel();
            channel.write("ok...");
        }
     
    }
    TestNettyClient
    package com.zbb.test.netty;
     
    import java.net.InetSocketAddress;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
     
    import org.jboss.netty.bootstrap.ClientBootstrap;
    import org.jboss.netty.channel.Channel;
    import org.jboss.netty.channel.ChannelFuture;
    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.ExceptionEvent;
    import org.jboss.netty.channel.MessageEvent;
    import org.jboss.netty.channel.SimpleChannelHandler;
    import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
    import org.jboss.netty.handler.codec.string.StringDecoder;
    import org.jboss.netty.handler.codec.string.StringEncoder;
     
    public class TestNettyClient {
     
        public static void main(String[] args) {
            // 创建netty client
            ClientBootstrap clientBootstrap = new ClientBootstrap();
            // 创建 两个线程池
            ExecutorService pool1 = Executors.newCachedThreadPool();
            ExecutorService pool2 = Executors.newCachedThreadPool();
            // 创建线程工程
            clientBootstrap.setFactory(new NioClientSocketChannelFactory(pool1, pool2));
            // 创建管道工厂
            clientBootstrap.setPipelineFactory(new ChannelPipelineFactory() {
     
                public ChannelPipeline getPipeline() throws Exception {
                    ChannelPipeline pipeline = org.jboss.netty.channel.Channels.pipeline();
                    pipeline.addLast("encode", new StringEncoder());
                    pipeline.addLast("decode", new StringDecoder());
                    pipeline.addLast("clientHandler", new ZbbNettyHandlerClient());
     
                    // TODO Auto-generated method stub
                    return pipeline;
                }
            });
            // 绑定端口
            ChannelFuture connect = clientBootstrap.connect(new InetSocketAddress("127.0.0.1", 8080));
            System.out.println("client start ...");
            Channel channel = connect.getChannel();
            channel.write("aaaaaaaaaa");
        }
     
    }
     
    class ZbbNettyHandlerClient extends SimpleChannelHandler {
     
        @Override
        public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
            // TODO Auto-generated method stub
            super.channelClosed(ctx, e);
            System.out.println("channelClosed");
        }
     
        @Override
        public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
            // TODO Auto-generated method stub
            super.channelDisconnected(ctx, e);
            System.out.println("channelDisconnected");
        }
     
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
            // TODO Auto-generated method stub
            super.exceptionCaught(ctx, e);
            System.out.println("exceptionCaught");
        }
     
        @Override
        public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
            // TODO Auto-generated method stub
            super.messageReceived(ctx, e);
            System.out.println("messageReceived");
            System.out.println("client received server message:" + e.getMessage());
        }
     
    }
  • 相关阅读:
    双USB墙壁电源插座面板....制作详解-电子产品世界论坛
    公共充电站?不用守着手机充电了 | 爱范儿
    公司简介-远能电气
    连接器|网络滤波连接器|电脑连接器|RJ45变压器-华联威电子有限公司
    自己动手做简单移动电源
    移动电源市场有多乱?
    聚合物电池_百度百科
    电芯:聚合物的一定比18650更好_移动电源_移动电源评测-中关村在线
    图文:充电宝租借自助机现身江城_新浪财经_新浪网
    不按时归还,信用会有“污点”_新浪浙江_新浪网
  • 原文地址:https://www.cnblogs.com/super-admin/p/9732912.html
Copyright © 2011-2022 走看看