zoukankan      html  css  js  c++  java
  • 六、netty tcp客户端

    所有文章

    https://www.cnblogs.com/lay2017/p/12922074.html

    正文

    除了服务端,netty还可以构建客户端。客户端你需要

    1.创建EventLoopGroup

    2.配置Bootstrap

    3.创建ChannelInitializer

    4.启动

    示例代码如下

    EventLoopGroup group = new NioEventLoopGroup();
    try{
        Bootstrap clientBootstrap = new Bootstrap();
    
        clientBootstrap.group(group);
        clientBootstrap.channel(NioSocketChannel.class);
        clientBootstrap.remoteAddress(new InetSocketAddress("localhost", 9999));
        clientBootstrap.handler(new ChannelInitializer<SocketChannel>() {
            protected void initChannel(SocketChannel socketChannel) throws Exception {
                socketChannel.pipeline().addLast(new ClientHandler());
            }
        });
        ChannelFuture channelFuture = clientBootstrap.connect().sync();
        channelFuture.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully().sync();
    }

    创建EventLoopGroup

    第一步是创建EventLoopGroup,相对比较简单,如

    EventLoopGroup group = new NioEventLoopGroup();

    创建并配置Bootstrap

    和ServerBootstrap类似,客户端Bootstrap

    Bootstrap clientBootstrap = new Bootstrap();

    还需要配置一下

    clientBootstrap.group(group);
    clientBootstrap.channel(NioSocketChannel.class);
    clientBootstrap.remoteAddress(new InetSocketAddress("localhost", 9999));

    配置了EventLoopGroup,指定NioSocketChannel,以及server的地址。

    创建ChannelInitializer

    第三步是创建ChannelInitializer

    clientBootstrap.handler(new ChannelInitializer<SocketChannel>() {
        protected void initChannel(SocketChannel socketChannel) throws Exception {
            socketChannel.pipeline().addLast(new ClientHandler());
        }
    });

    绑定到bootstrap上,并给pipeline添加ClientHandler

    启动

    最后一步是启动

    ChannelFuture channelFuture = clientBootstrap.connect().sync();

    将会连接到服务端,并同步等待连接完成

    如果想等待关闭,这样写

    channelFuture.channel().closeFuture().sync();

    ClientHandler

    最后给出ClientHandler

    public class ClientHandler extends SimpleChannelInboundHandler {
    
        @Override
        public void channelActive(ChannelHandlerContext channelHandlerContext){
            channelHandlerContext.writeAndFlush(Unpooled.copiedBuffer("Netty Rocks!", CharsetUtil.UTF_8));
        }
    
        @Override
        public void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf in) {
            System.out.println("Client received: " + in.toString(CharsetUtil.UTF_8));
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable cause){
            cause.printStackTrace();
            channelHandlerContext.close();
        }
    }
  • 相关阅读:
    Windows性能计数器应用
    Azure Oracle Linux VNC 配置
    Azure 配置管理系列 Oracle Linux (PART6)
    Azure 配置管理系列 Oracle Linux (PART5)
    Azure 配置管理系列 Oracle Linux (PART4)
    Azure 配置管理系列 Oracle Linux (PART3)
    Azure 配置管理系列 Oracle Linux (PART2)
    vagrant多节点配置
    docker基本操作
    LINUX开启允许对外访问的网络端口命令
  • 原文地址:https://www.cnblogs.com/lay2017/p/12922743.html
Copyright © 2011-2022 走看看