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