001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.net.examples.telnet;
019
020import java.io.IOException;
021
022import org.apache.commons.net.examples.util.IOUtil;
023import org.apache.commons.net.telnet.TelnetClient;
024
025/**
026 * This is an example of a trivial use of the TelnetClient class. It connects to the weather server at the University of Michigan, um-weather.sprl.umich.edu
027 * port 3000, and allows the user to interact with the server via standard input. You could use this example to connect to any telnet server, but it is
028 * obviously not general purpose because it reads from standard input a line at a time, making it inconvenient for use with a remote interactive shell. The
029 * TelnetClient class used by itself is mostly intended for automating access to telnet resources rather than interactive use.
030 */
031
032// This class requires the IOUtil support class!
033public final class WeatherTelnet {
034
035    public static void main(final String[] args) {
036        final TelnetClient telnet;
037
038        telnet = new TelnetClient();
039
040        try {
041            telnet.connect("rainmaker.wunderground.com", 3000);
042        } catch (final IOException e) {
043            e.printStackTrace();
044            System.exit(1);
045        }
046
047        IOUtil.readWrite(telnet.getInputStream(), telnet.getOutputStream(), System.in, System.out);
048
049        try {
050            telnet.disconnect();
051        } catch (final IOException e) {
052            e.printStackTrace();
053            System.exit(1);
054        }
055
056        System.exit(0);
057    }
058
059}