content: package com.lgp.netty.netty_ch1;
  
  import io.netty.bootstrap.Bootstrap;
  import io.netty.channel.ChannelFuture;
  import io.netty.channel.ChannelInitializer;
  import io.netty.channel.EventLoopGroup;
  import io.netty.channel.nio.NioEventLoopGroup;
  import io.netty.channel.socket.SocketChannel;
  import io.netty.channel.socket.nio.NioSocketChannel;
  
  import java.net.InetSocketAddress;
  
  /**
   * @AUTHOR lgp
   * @DATE 2018/10/24 17:14
   * @DESCRIPTION 请先看EchoServer和 EchoServerHandler
   **/
  public class EchoClient {
      /*服务器的地址*/
      public final String host;
      public final int port;
  
      public EchoClient(String host, int port) {
          this.host = host;
          this.port = port;
      }
  
      public static void main(String[] args) throws Exception {
          new EchoClient("127.0.0.1", 1234).start();
      }
  
      public void start() throws Exception {
          EventLoopGroup group = new NioEventLoopGroup();
          try {
              /*注意client是Bootstrap，server是ServerBootstrap*/
              Bootstrap b = new Bootstrap();
              b.group(group)
                      .channel(NioSocketChannel.class)
                      //1 配置远程的服务器地址
                      .remoteAddress(new InetSocketAddress(host, port))
                      .handler(new ChannelInitializer<SocketChannel>() {
                          @Override
                          public void initChannel(SocketChannel ch) throws Exception {
                              ch.pipeline().addLast(new EchoClientHandler());
                          }
                      });
              //2 连接服务器，并等待连接完成
              ChannelFuture f = b.connect().sync();
              f.channel().closeFuture().sync();
          } finally {
              group.shutdownGracefully().sync();
          }
      }
  }
  