• 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();
    }
    }
    }

    新手教程,欢迎拍砖~


  • 相关阅读:
    HttpServletRequest对象,自己学习的心得。
    @PathVariable注解的使用和@Requestparam
    微信小程序授权 获取用户的openid和session_key【后端使用java语言编写】,我写的是get方式,目的是测试能否获取到微信服务器中的数据,后期我会写上post请求方式。
    (转)ubuntu 14.04下安装hadoop2.6(伪分布式)
    数据库范式
    C与Python变量的区别
    python3获取当前目录(转)
    最大连续子数组积的线性解法
    最大子数组的线性解法
    windows7下python3.4.3 添加库路径(转)
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/7266819.html
Copyright © 2020-2023  润新知