zoukankan      html  css  js  c++  java
  • zbb20180930 java,nio,netty Netty5.0用法

    Netty5.0用法

    Maven坐标

          <dependencies>

               <!-- https://mvnrepository.com/artifact/io.netty/netty-all -->

               <dependency>

                     <groupId>io.netty</groupId>

                     <artifactId>netty-all</artifactId>

                     <version>5.0.0.Alpha2</version>

               </dependency>

     

               <!-- https://mvnrepository.com/artifact/org.jboss.marshalling/jboss-marshalling -->

               <dependency>

                     <groupId>org.jboss.marshalling</groupId>

                     <artifactId>jboss-marshalling</artifactId>

                     <version>1.3.19.GA</version>

               </dependency>

               <!-- https://mvnrepository.com/artifact/org.jboss.marshalling/jboss-marshalling-serial -->

               <dependency>

                     <groupId>org.jboss.marshalling</groupId>

                     <artifactId>jboss-marshalling-serial</artifactId>

                     <version>1.3.18.GA</version>

                     <scope>test</scope>

               </dependency>

     

          </dependencies>

    创建服务器端

    class ServerHandler extends ChannelHandlerAdapter {

         /**

          * 当通道被调用,执行该方法

          */

         @Override

         publicvoid channelRead(ChannelHandlerContext ctx, Object msgthrows Exception {

             // 接收数据

             String value = (String) msg;

             System.out.println("Server msg:" + value);

             // 回复给客户端 “您好!”

             String res = "好的...";

             ctx.writeAndFlush(Unpooled.copiedBuffer(res.getBytes()));

         }

     

    }

     

    publicclass NettyServer {

     

         publicstaticvoid main(String[] argsthrows InterruptedException {

             System.out.println("服务器端已经启动....");

             // 1.创建2个线程,一个负责接收客户端连接, 一个负责进行 传输数据

             NioEventLoopGroup pGroup = new NioEventLoopGroup();

             NioEventLoopGroup cGroup = new NioEventLoopGroup();

             // 2. 创建服务器辅助类

             ServerBootstrap b = new ServerBootstrap();

             b.group(pGroupcGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 1024)

                       // 3.设置缓冲区与发送区大小

                       .option(ChannelOption.SO_SNDBUF, 32 * 1024).option(ChannelOption.SO_RCVBUF, 32 * 1024)

                       .childHandler(new ChannelInitializer<SocketChannel>() {

                           @Override

                           protectedvoid initChannel(SocketChannel scthrows Exception {

                                sc.pipeline().addLast(new StringDecoder());

                                sc.pipeline().addLast(new ServerHandler());

                           }

                       });

             ChannelFuture cf = b.bind(8080).sync();

             cf.channel().closeFuture().sync();

             pGroup.shutdownGracefully();

             cGroup.shutdownGracefully();

        

         }

     

    }

     

     

     

    创建客户端

    class ClientHandler extends ChannelHandlerAdapter {

     

         /**

          * 当通道被调用,执行该方法

          */

         @Override

         publicvoid channelRead(ChannelHandlerContext ctx, Object msgthrows Exception {

             // 接收数据

             String value = (String) msg;

             System.out.println("client msg:" + value);

         }

     

        

    }

     

    publicclass NettyClient {

     

         publicstaticvoid main(String[] argsthrows InterruptedException {

             System.out.println("客户端已经启动....");

             // 创建负责接收客户端连接

             NioEventLoopGroup pGroup = new NioEventLoopGroup();

             Bootstrap b = new Bootstrap();

             b.group(pGroup).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {

                  @Override

                  protectedvoid initChannel(SocketChannel scthrows Exception {

                       sc.pipeline().addLast(new StringDecoder());

                       sc.pipeline().addLast(new ClientHandler());

                  }

             });

             ChannelFuture cf = b.connect("127.0.0.1", 8080).sync();

              cf.channel().writeAndFlush(Unpooled.wrappedBuffer("itmayiedu".getBytes()));

              cf.channel().writeAndFlush(Unpooled.wrappedBuffer("itmayiedu".getBytes()));

             // 等待客户端端口号关闭

             cf.channel().closeFuture().sync();

             pGroup.shutdownGracefully();

     

         }

     

    }

     

     

     
  • 相关阅读:
    重磅官宣:Nacos2.0发布,性能提升10倍
    埃森哲携手阿里云共建基于云原生的消费者运营中台解决方案
    3. Windows根据端口查进程---ADB 相关报错 ADB server didn't ACK cannot bind ':5037'
    2.Could not open Selected VM debug port (8700). Make sure you do not have another instance of DDMS or of the eclipse plugin running
    1.运行Android Studio,一直提示:Error running app: Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled.
    SpringBoot_配置-yaml配置文件值获取
    SpringBoot_入门-使用向导快速创建Spring Boot应用
    SpringBoot_入门-HelloWorld细节-自动配置
    SpringBoot_入门-微服务简介
    SpringBoot_入门-Spring Boot简介
  • 原文地址:https://www.cnblogs.com/super-admin/p/9733030.html
Copyright © 2011-2022 走看看