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.MultiThreadIoEventLoopGroup;
25 import io.netty.channel.nio.NioIoHandler;
26 import io.netty.channel.socket.SocketChannel;
27 import io.netty.channel.socket.nio.NioSocketChannel;
28 import io.netty.example.util.ServerUtil;
29 import io.netty.handler.ssl.SslContext;
30
31
32
33
34
35
36
37 public final class EchoClient {
38
39 static final String HOST = System.getProperty("host", "127.0.0.1");
40 static final int PORT = Integer.parseInt(System.getProperty("port", "8007"));
41 static final int SIZE = Integer.parseInt(System.getProperty("size", "256"));
42
43 public static void main(String[] args) throws Exception {
44
45 final SslContext sslCtx = ServerUtil.buildSslContext();
46
47
48 EventLoopGroup group = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
49 try {
50 Bootstrap b = new Bootstrap();
51 b.group(group)
52 .channel(NioSocketChannel.class)
53 .option(ChannelOption.TCP_NODELAY, true)
54 .handler(new ChannelInitializer<SocketChannel>() {
55 @Override
56 public void initChannel(SocketChannel ch) throws Exception {
57 ChannelPipeline p = ch.pipeline();
58 if (sslCtx != null) {
59 p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
60 }
61
62 p.addLast(new EchoClientHandler());
63 }
64 });
65
66
67 ChannelFuture f = b.connect(HOST, PORT).sync();
68
69
70 f.channel().closeFuture().sync();
71 } finally {
72
73 group.shutdownGracefully();
74 }
75 }
76 }