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    
018    package examples.telnet;
019    
020    import java.io.IOException;
021    import org.apache.commons.net.telnet.TelnetClient;
022    
023    import examples.util.IOUtil;
024    
025    /***
026     * This is an example of a trivial use of the TelnetClient class.
027     * It connects to the weather server at the University of Michigan,
028     * um-weather.sprl.umich.edu port 3000, and allows the user to interact
029     * with the server via standard input.  You could use this example to
030     * connect to any telnet server, but it is obviously not general purpose
031     * because it reads from standard input a line at a time, making it
032     * inconvenient for use with a remote interactive shell.  The TelnetClient
033     * class used by itself is mostly intended for automating access to telnet
034     * resources rather than interactive use.
035     * <p>
036     ***/
037    
038    // This class requires the IOUtil support class!
039    public final class WeatherTelnet
040    {
041    
042        public final static void main(String[] args)
043        {
044            TelnetClient telnet;
045    
046            telnet = new TelnetClient();
047    
048            try
049            {
050                telnet.connect("rainmaker.wunderground.com", 3000);
051            }
052            catch (IOException e)
053            {
054                e.printStackTrace();
055                System.exit(1);
056            }
057    
058            IOUtil.readWrite(telnet.getInputStream(), telnet.getOutputStream(),
059                             System.in, System.out);
060    
061            try
062            {
063                telnet.disconnect();
064            }
065            catch (IOException e)
066            {
067                e.printStackTrace();
068                System.exit(1);
069            }
070    
071            System.exit(0);
072        }
073    
074    }
075    
076