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