View Javadoc

    1   /*
    2    * Copyright 2001-2005 The Apache Software Foundation
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * 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,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package examples;
    17
    18  import java.io.IOException;
    19  import org.apache.commons.net.telnet.TelnetClient;
    20
    21  /****
    22   * This is an example of a trivial use of the TelnetClient class.
    23   * It connects to the weather server at the University of Michigan,
    24   * um-weather.sprl.umich.edu port 3000, and allows the user to interact
    25   * with the server via standard input.  You could use this example to
    26   * connect to any telnet server, but it is obviously not general purpose
    27   * because it reads from standard input a line at a time, making it
    28   * inconvenient for use with a remote interactive shell.  The TelnetClient
    29   * class used by itself is mostly intended for automating access to telnet
    30   * resources rather than interactive use.
    31   * <p>
    32   ***/
    33
    34  // This class requires the IOUtil support class!
    35  public final class weatherTelnet
    36  {
    37
    38      public final static void main(String[] args)
    39      {
    40          TelnetClient telnet;
    41
    42          telnet = new TelnetClient();
    43
    44          try
    45          {
    46              telnet.connect("rainmaker.wunderground.com", 3000);
    47          }
    48          catch (IOException e)
    49          {
    50              e.printStackTrace();
    51              System.exit(1);
    52          }
    53
    54          IOUtil.readWrite(telnet.getInputStream(), telnet.getOutputStream(),
    55                           System.in, System.out);
    56
    57          try
    58          {
    59              telnet.disconnect();
    60          }
    61          catch (IOException e)
    62          {
    63              e.printStackTrace();
    64              System.exit(1);
    65          }
    66
    67          System.exit(0);
    68      }
    69
    70  }
    71
    72