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;
019    
020    import java.io.BufferedReader;
021    import java.io.IOException;
022    import java.io.InputStreamReader;
023    import java.io.InterruptedIOException;
024    import java.io.OutputStreamWriter;
025    import java.io.PrintWriter;
026    import java.net.InetAddress;
027    import java.net.SocketException;
028    
029    import org.apache.commons.net.echo.EchoTCPClient;
030    import org.apache.commons.net.echo.EchoUDPClient;
031    
032    /***
033     * This is an example program demonstrating how to use the EchoTCPClient
034     * and EchoUDPClient classes.  This program connects to the default echo
035     * service port of a specified server, then reads lines from standard
036     * input, writing them to the echo server, and then printing the echo.
037     * The default is to use the TCP port.  Use the -udp flag to use the UDP
038     * port.
039     * <p>
040     * Usage: echo [-udp] <hostname>
041     * <p>
042     ***/
043    public final class echo
044    {
045    
046        public static final void echoTCP(String host) throws IOException
047        {
048            EchoTCPClient client = new EchoTCPClient();
049            BufferedReader input, echoInput;
050            PrintWriter echoOutput;
051            String line;
052    
053            // We want to timeout if a response takes longer than 60 seconds
054            client.setDefaultTimeout(60000);
055            client.connect(host);
056            System.out.println("Connected to " + host + ".");
057            input = new BufferedReader(new InputStreamReader(System.in));
058            echoOutput =
059                new PrintWriter(new OutputStreamWriter(client.getOutputStream()), true);
060            echoInput =
061                new BufferedReader(new InputStreamReader(client.getInputStream()));
062    
063            while ((line = input.readLine()) != null)
064            {
065                echoOutput.println(line);
066                System.out.println(echoInput.readLine());
067            }
068    
069            client.disconnect();
070        }
071    
072        public static final void echoUDP(String host) throws IOException
073        {
074            int length, count;
075            byte[] data;
076            String line;
077            BufferedReader input;
078            InetAddress address;
079            EchoUDPClient client;
080    
081            input = new BufferedReader(new InputStreamReader(System.in));
082            address = InetAddress.getByName(host);
083            client = new EchoUDPClient();
084    
085            client.open();
086            // If we don't receive an echo within 5 seconds, assume the packet is lost.
087            client.setSoTimeout(5000);
088            System.out.println("Ready to echo to " + host + ".");
089    
090            // Remember, there are no guarantees about the ordering of returned
091            // UDP packets, so there is a chance the output may be jumbled.
092            while ((line = input.readLine()) != null)
093            {
094                data = line.getBytes();
095                client.send(data, address);
096                count = 0;
097                do
098                {
099                    try
100                    {
101                        length = client.receive(data);
102                    }
103                    // Here we catch both SocketException and InterruptedIOException,
104                    // because even though the JDK 1.1 docs claim that
105                    // InterruptedIOException is thrown on a timeout, it seems
106                    // SocketException is also thrown.
107                    catch (SocketException e)
108                    {
109                        // We timed out and assume the packet is lost.
110                        System.err.println(
111                            "SocketException: Timed out and dropped packet");
112                        break;
113                    }
114                    catch (InterruptedIOException e)
115                    {
116                        // We timed out and assume the packet is lost.
117                        System.err.println(
118                            "InterruptedIOException: Timed out and dropped packet");
119                        break;
120                    }
121                    System.out.print(new String(data, 0, length));
122                    count += length;
123                }
124                while (count < data.length);
125    
126                System.out.println();
127            }
128    
129            client.close();
130        }
131    
132    
133        public static final void main(String[] args)
134        {
135    
136            if (args.length == 1)
137            {
138                try
139                {
140                    echoTCP(args[0]);
141                }
142                catch (IOException e)
143                {
144                    e.printStackTrace();
145                    System.exit(1);
146                }
147            }
148            else if (args.length == 2 && args[0].equals("-udp"))
149            {
150                try
151                {
152                    echoUDP(args[1]);
153                }
154                catch (IOException e)
155                {
156                    e.printStackTrace();
157                    System.exit(1);
158                }
159            }
160            else
161            {
162                System.err.println("Usage: echo [-udp] <hostname>");
163                System.exit(1);
164            }
165    
166        }
167    
168    }
169