zoukankan      html  css  js  c++  java
  • netty4简易教程

    近来有空,研究了一下netty。netty5还仅仅是beta版本号。官方建议使用netty4。

    这个实例中,Client通过控制台输入信息,与Server端通信。

    对于普通开发人员来说,netty4的经常使用接口有:SimpleChannelInboundHandler/SimpleChannelOutboundHandler、ChannelInitializer、encoder和decoder相关的类。

    server端:

    1、编写handler类

    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.SimpleChannelInboundHandler;

    public class ServerHandler extends SimpleChannelInboundHandler {
    @Override
    public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    System.out.println("server:" + "channelRead:" + msg);
    ctx.writeAndFlush("Received your message !");
    }
    }

    2、注冊服务。绑定port:

    import io.netty.channel.Channel;
    import io.netty.channel.ChannelFuture;
    import io.netty.channel.ChannelInitializer;
    import io.netty.channel.ChannelOption;
    import io.netty.channel.EventLoopGroup;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.nio.NioServerSocketChannel;
    import io.netty.handler.codec.protobuf.ProtobufDecoder;
    import io.netty.handler.codec.protobuf.ProtobufEncoder;
    import io.netty.handler.codec.string.StringDecoder;
    import io.netty.handler.codec.string.StringEncoder;

    public class Server {


    public static void main(String[] args) {
    EventLoopGroup bossEventLoopGroup = new NioEventLoopGroup();
    EventLoopGroup workerEventLoopGroup = new NioEventLoopGroup();
    try {
    ServerBootstrap serverBootstrap = new ServerBootstrap();
    serverBootstrap.group(bossEventLoopGroup, workerEventLoopGroup);
    serverBootstrap.channel(NioServerSocketChannel.class);
    serverBootstrap.childHandler(new ChannelInitializer<Channel>() {
    @Override
    protected void initChannel(Channel ch) throws Exception {
    ch.pipeline().addLast("encoder", new StringEncoder());
    ch.pipeline().addLast("decoder", new StringDecoder());
    ch.pipeline().addLast("handler", new ServerHandler());
    };
    });
    serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
    ChannelFuture channelFuture = serverBootstrap.bind(8888).sync();
    channelFuture.channel().closeFuture().sync();
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    bossEventLoopGroup.shutdownGracefully();
    workerEventLoopGroup.shutdownGracefully();
    }
    }
    }


    client:

    1、编写handler类

    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.SimpleChannelInboundHandler;

    public class ClientHandler extends SimpleChannelInboundHandler {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    System.out.println("client:" + "channelRead:" + msg);
    }
    }

    2、编写请求入口

    import java.io.BufferedReader;
    import java.io.InputStreamReader;

    import io.netty.bootstrap.Bootstrap;
    import io.netty.channel.Channel;
    import io.netty.channel.ChannelInitializer;
    import io.netty.channel.EventLoopGroup;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.nio.NioSocketChannel;
    import io.netty.handler.codec.protobuf.ProtobufDecoder;
    import io.netty.handler.codec.protobuf.ProtobufEncoder;
    import io.netty.handler.codec.string.StringDecoder;
    import io.netty.handler.codec.string.StringEncoder;

    public class Client {

    public static void main(String[] args) {
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    try {
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(eventLoopGroup);
    bootstrap.channel(NioSocketChannel.class);
    bootstrap.handler(new ChannelInitializer<Channel>() {
    @Override
    protected void initChannel(Channel ch) throws Exception {
    ch.pipeline().addLast("encoder", new StringEncoder());
    ch.pipeline().addLast("decoder", new StringDecoder());
    ch.pipeline().addLast("handler", new ClientHandler());
    };
    });

    Channel ch = bootstrap.connect("127.0.0.1", 8888).sync().channel();

    // 控制台输入
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    for (;;) {
    String line = in.readLine();
    if (line == null || "".equals(line)) {
    continue;
    }
    ch.writeAndFlush(line);
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    eventLoopGroup.shutdownGracefully();
    }
    }
    }

    新手教程,欢迎拍砖~


  • 相关阅读:
    Static Fields and Methods
    usermod
    理解Java的Class类、"this."关键字、Constructor构造器(一)
    [REPRINT] Java 101: Classes and objects in Java
    第六章 Java并发容器和框架
    第五章 Java中锁
    第四章 Java并发编程基础
    第三章 Java内存模型(下)
    第三章 Java内存模型(上)
    第二章 并发机制的底层实现原理
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/7266819.html
Copyright © 2011-2022 走看看