查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.example.discard;
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   * Keeps sending random data to the specified address.
32   */
33  public final class DiscardClient {
34  
35      static final boolean SSL = System.getProperty("ssl") != null;
36      static final String HOST = System.getProperty("host", "127.0.0.1");
37      static final int PORT = Integer.parseInt(System.getProperty("port", "8009"));
38      static final int SIZE = Integer.parseInt(System.getProperty("size", "256"));
39  
40      public static void main(String[] args) throws Exception {
41          // Configure SSL.
42          final SslContext sslCtx;
43          if (SSL) {
44              sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
45          } else {
46              sslCtx = null;
47          }
48  
49          // Configure the bootstrap.
50          ClientBootstrap bootstrap = new ClientBootstrap(
51                  new NioClientSocketChannelFactory(
52                          Executors.newCachedThreadPool(),
53                          Executors.newCachedThreadPool()));
54  
55          try {
56              bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
57                  public ChannelPipeline getPipeline() {
58                      ChannelPipeline p = Channels.pipeline();
59                      if (sslCtx != null) {
60                          p.addLast("ssl", sslCtx.newHandler(HOST, PORT));
61                      }
62                      p.addLast("discard", new DiscardClientHandler());
63                      return p;
64                  }
65              });
66  
67              // Start the connection attempt.
68              ChannelFuture future = bootstrap.connect(new InetSocketAddress(HOST, PORT));
69  
70              // Wait until the connection is closed or the connection attempt fails.
71              future.getChannel().getCloseFuture().sync();
72          } finally {
73              // Shut down thread pools to exit.
74              bootstrap.releaseExternalResources();
75          }
76      }
77  }