View Javadoc

    1   /*
    2    * Copyright 2009 Red Hat, Inc.
    3    *
    4    * Red Hat licenses this file to you under the Apache License, version 2.0
    5    * (the "License"); you may not use this file except in compliance with the
    6    * 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.echo;
    17
    18  import java.net.InetSocketAddress;
    19  import java.util.concurrent.Executors;
    20
    21  import org.jboss.netty.bootstrap.ClientBootstrap;
    22  import org.jboss.netty.channel.ChannelFuture;
    23  import org.jboss.netty.channel.ChannelPipeline;
    24  import org.jboss.netty.channel.ChannelPipelineFactory;
    25  import org.jboss.netty.channel.Channels;
    26  import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
    27
    28  /**
    29   * Sends one message when a connection is open and echoes back any received
    30   * data to the server.  Simply put, the echo client initiates the ping-pong
    31   * traffic between the echo client and server by sending the first message to
    32   * the server.
    33   *
    34   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    35   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
    36   *
    37   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
    38   *
    39   */
    40  public class EchoClient {
    41
    42      public static void main(String[] args) throws Exception {
    43          // Print usage if no argument is specified.
    44          if (args.length < 2 || args.length > 3) {
    45              System.err.println(
    46                      "Usage: " + EchoClient.class.getSimpleName() +
    47                      " <host> <port> [<first message size>]");
    48              return;
    49          }
    50
    51          // Parse options.
    52          final String host = args[0];
    53          final int port = Integer.parseInt(args[1]);
    54          final int firstMessageSize;
    55          if (args.length == 3) {
    56              firstMessageSize = Integer.parseInt(args[2]);
    57          } else {
    58              firstMessageSize = 256;
    59          }
    60
    61          // Configure the client.
    62          ClientBootstrap bootstrap = new ClientBootstrap(
    63                  new NioClientSocketChannelFactory(
    64                          Executors.newCachedThreadPool(),
    65                          Executors.newCachedThreadPool()));
    66
    67          // Set up the pipeline factory.
    68          bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
    69              public ChannelPipeline getPipeline() throws Exception {
    70                  return Channels.pipeline(
    71                          new EchoClientHandler(firstMessageSize));
    72              }
    73          });
    74
    75          // Start the connection attempt.
    76          ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
    77
    78          // Wait until the connection is closed or the connection attempt fails.
    79          future.getChannel().getCloseFuture().awaitUninterruptibly();
    80
    81          // Shut down thread pools to exit.
    82          bootstrap.releaseExternalResources();
    83      }
    84  }