1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.example.echo;
17
18 import io.netty.bootstrap.Bootstrap;
19 import io.netty.channel.ChannelFuture;
20 import io.netty.channel.ChannelInitializer;
21 import io.netty.channel.ChannelOption;
22 import io.netty.channel.ChannelPipeline;
23 import io.netty.channel.EventLoopGroup;
24 import io.netty.channel.nio.NioEventLoopGroup;
25 import io.netty.channel.socket.SocketChannel;
26 import io.netty.channel.socket.nio.NioSocketChannel;
27 import io.netty.example.util.ServerUtil;
28 import io.netty.handler.ssl.SslContext;
29
30
31
32
33
34
35
36 public final class EchoClient {
37
38 static final String HOST = System.getProperty("host", "127.0.0.1");
39 static final int PORT = Integer.parseInt(System.getProperty("port", "8007"));
40 static final int SIZE = Integer.parseInt(System.getProperty("size", "256"));
41
42 public static void main(String[] args) throws Exception {
43
44 final SslContext sslCtx = ServerUtil.buildSslContext();
45
46
47 EventLoopGroup group = new NioEventLoopGroup();
48 try {
49 Bootstrap b = new Bootstrap();
50 b.group(group)
51 .channel(NioSocketChannel.class)
52 .option(ChannelOption.TCP_NODELAY, true)
53 .handler(new ChannelInitializer<SocketChannel>() {
54 @Override
55 public void initChannel(SocketChannel ch) throws Exception {
56 ChannelPipeline p = ch.pipeline();
57 if (sslCtx != null) {
58 p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
59 }
60
61 p.addLast(new EchoClientHandler());
62 }
63 });
64
65
66 ChannelFuture f = b.connect(HOST, PORT).sync();
67
68
69 f.channel().closeFuture().sync();
70 } finally {
71
72 group.shutdownGracefully();
73 }
74 }
75 }