echo.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.net.examples.unix;

  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStreamReader;
  21. import java.io.InterruptedIOException;
  22. import java.io.OutputStreamWriter;
  23. import java.io.PrintWriter;
  24. import java.net.InetAddress;
  25. import java.net.SocketException;
  26. import java.nio.charset.Charset;
  27. import java.time.Duration;

  28. import org.apache.commons.net.echo.EchoTCPClient;
  29. import org.apache.commons.net.echo.EchoUDPClient;

  30. /**
  31.  * 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
  32.  * 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
  33.  * the -udp flag to use the UDP port.
  34.  * <p>
  35.  * Usage: echo [-udp] <hostname>
  36.  * </p>
  37.  */
  38. public final class echo {

  39.     public static void echoTCP(final String host) throws IOException {
  40.         final EchoTCPClient client = new EchoTCPClient();
  41.         String line;

  42.         // We want to timeout if a response takes longer than 60 seconds
  43.         client.setDefaultTimeout(60000);
  44.         client.connect(host);
  45.         try {
  46.             System.out.println("Connected to " + host + ".");
  47.             final Charset charset = Charset.defaultCharset();
  48.             final BufferedReader input = new BufferedReader(new InputStreamReader(System.in, charset));
  49.             try (PrintWriter echoOutput = new PrintWriter(new OutputStreamWriter(client.getOutputStream(), charset), true);
  50.                     BufferedReader echoInput = new BufferedReader(new InputStreamReader(client.getInputStream(), charset))) {
  51.                 while ((line = input.readLine()) != null) {
  52.                     echoOutput.println(line);
  53.                     System.out.println(echoInput.readLine());
  54.                 }
  55.             }
  56.             // Don't close System.in
  57.         } finally {
  58.             client.disconnect();
  59.         }
  60.     }

  61.     public static void echoUDP(final String host) throws IOException {
  62.         int length, count;
  63.         byte[] data;
  64.         String line;
  65.         final BufferedReader input;
  66.         final InetAddress address;

  67.         final Charset charset = Charset.defaultCharset();
  68.         input = new BufferedReader(new InputStreamReader(System.in, charset));
  69.         address = InetAddress.getByName(host);
  70.         try (EchoUDPClient client = new EchoUDPClient()) {

  71.             client.open();
  72.             // If we don't receive an echo within 5 seconds, assume the packet is lost.
  73.             client.setSoTimeout(Duration.ofSeconds(5));
  74.             System.out.println("Ready to echo to " + host + ".");

  75.             // Remember, there are no guarantees about the ordering of returned
  76.             // UDP packets, so there is a chance the output may be jumbled.
  77.             while ((line = input.readLine()) != null) {
  78.                 data = line.getBytes(charset);
  79.                 client.send(data, address);
  80.                 count = 0;
  81.                 do {
  82.                     try {
  83.                         length = client.receive(data);
  84.                     }
  85.                     // Here we catch both SocketException and InterruptedIOException,
  86.                     // because even though the JDK 1.1 docs claim that
  87.                     // InterruptedIOException is thrown on a timeout, it seems
  88.                     // SocketException is also thrown.
  89.                     catch (final SocketException e) {
  90.                         // We timed out and assume the packet is lost.
  91.                         System.err.println("SocketException: Timed out and dropped packet");
  92.                         break;
  93.                     } catch (final InterruptedIOException e) {
  94.                         // We timed out and assume the packet is lost.
  95.                         System.err.println("InterruptedIOException: Timed out and dropped packet");
  96.                         break;
  97.                     }
  98.                     System.out.print(new String(data, 0, length, charset));
  99.                     count += length;
  100.                 } while (count < data.length);

  101.                 System.out.println();
  102.             }

  103.         }
  104.     }

  105.     public static void main(final String[] args) {

  106.         if (args.length == 1) {
  107.             try {
  108.                 echoTCP(args[0]);
  109.             } catch (final IOException e) {
  110.                 e.printStackTrace();
  111.                 System.exit(1);
  112.             }
  113.         } else if (args.length == 2 && args[0].equals("-udp")) {
  114.             try {
  115.                 echoUDP(args[1]);
  116.             } catch (final IOException e) {
  117.                 e.printStackTrace();
  118.                 System.exit(1);
  119.             }
  120.         } else {
  121.             System.err.println("Usage: echo [-udp] <hostname>");
  122.             System.exit(1);
  123.         }

  124.     }

  125. }