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

    新手教程,欢迎拍砖~


  • 相关阅读:
    【POJ 2044】 Weather Forecast
    【POJ 1703】 Find them,Catch them
    【SCOI 2005】 骑士精神
    字长与指针
    XModem协议
    SecureCRT乱码问题解决方法
    usb设备驱动程序
    如何检测 51单片机IO口的下降沿
    matlab神经网络工具箱创建神经网络
    九针串口接线问题, 232, 485
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/7266819.html
Copyright © 2020-2023  润新知