1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.example.echo;
17
18 import org.jboss.netty.bootstrap.ClientBootstrap;
19 import org.jboss.netty.channel.ChannelFuture;
20 import org.jboss.netty.channel.ChannelPipeline;
21 import org.jboss.netty.channel.ChannelPipelineFactory;
22 import org.jboss.netty.channel.Channels;
23 import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
24 import org.jboss.netty.handler.ssl.SslContext;
25 import org.jboss.netty.handler.ssl.util.InsecureTrustManagerFactory;
26
27 import java.net.InetSocketAddress;
28 import java.util.concurrent.Executors;
29
30
31
32
33
34
35
36 public final class EchoClient {
37
38 static final boolean SSL = System.getProperty("ssl") != null;
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;
46 if (SSL) {
47 sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
48 } else {
49 sslCtx = null;
50 }
51
52
53 ClientBootstrap bootstrap = new ClientBootstrap(
54 new NioClientSocketChannelFactory(
55 Executors.newCachedThreadPool(),
56 Executors.newCachedThreadPool()));
57
58 try {
59 bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
60 public ChannelPipeline getPipeline() {
61 ChannelPipeline p = Channels.pipeline();
62 if (sslCtx != null) {
63 p.addLast("ssl", sslCtx.newHandler(HOST, PORT));
64 }
65 p.addLast("echo", new EchoClientHandler());
66 return p;
67 }
68 });
69 bootstrap.setOption("tcpNoDelay", true);
70 bootstrap.setOption("receiveBufferSize", 1048576);
71 bootstrap.setOption("sendBufferSize", 1048576);
72
73
74 ChannelFuture future = bootstrap.connect(new InetSocketAddress(HOST, PORT));
75
76
77 future.getChannel().getCloseFuture().sync();
78 } finally {
79
80 bootstrap.releaseExternalResources();
81 }
82 }
83 }