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