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.telnet;
    17
    18  import java.io.BufferedReader;
    19  import java.io.InputStreamReader;
    20  import java.net.InetSocketAddress;
    21  import java.util.concurrent.Executors;
    22
    23  import org.jboss.netty.bootstrap.ClientBootstrap;
    24  import org.jboss.netty.channel.Channel;
    25  import org.jboss.netty.channel.ChannelFuture;
    26  import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
    27
    28  /**
    29   * Simplistic telnet client.
    30   *
    31   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    32   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
    33   *
    34   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
    35   */
    36  public class TelnetClient {
    37
    38      public static void main(String[] args) throws Exception {
    39          // Print usage if no argument is specified.
    40          if (args.length != 2) {
    41              System.err.println(
    42                      "Usage: " + TelnetClient.class.getSimpleName() +
    43                      " <host> <port>");
    44              return;
    45          }
    46
    47          // Parse options.
    48          String host = args[0];
    49          int port = Integer.parseInt(args[1]);
    50
    51          // Configure the client.
    52          ClientBootstrap bootstrap = new ClientBootstrap(
    53                  new NioClientSocketChannelFactory(
    54                          Executors.newCachedThreadPool(),
    55                          Executors.newCachedThreadPool()));
    56
    57          // Configure the pipeline factory.
    58          bootstrap.setPipelineFactory(new TelnetClientPipelineFactory());
    59
    60          // Start the connection attempt.
    61          ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
    62
    63          // Wait until the connection attempt succeeds or fails.
    64          Channel channel = future.awaitUninterruptibly().getChannel();
    65          if (!future.isSuccess()) {
    66              future.getCause().printStackTrace();
    67              bootstrap.releaseExternalResources();
    68              return;
    69          }
    70
    71          // Read commands from the stdin.
    72          ChannelFuture lastWriteFuture = null;
    73          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    74          for (;;) {
    75              String line = in.readLine();
    76              if (line == null) {
    77                  break;
    78              }
    79
    80              // Sends the received line to the server.
    81              lastWriteFuture = channel.write(line + "\r\n");
    82
    83              // If user typed the 'bye' command, wait until the server closes
    84              // the connection.
    85              if (line.toLowerCase().equals("bye")) {
    86                  channel.getCloseFuture().awaitUninterruptibly();
    87                  break;
    88              }
    89          }
    90
    91          // Wait until all messages are flushed before closing the channel.
    92          if (lastWriteFuture != null) {
    93              lastWriteFuture.awaitUninterruptibly();
    94          }
    95
    96          // Close the connection.  Make sure the close operation ends because
    97          // all I/O operations are asynchronous in Netty.
    98          channel.close().awaitUninterruptibly();
    99
    100         // Shut down all thread pools to exit.
    101         bootstrap.releaseExternalResources();
    102     }
    103 }