View Javadoc
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  
18  package org.apache.commons.net.examples.unix;
19  
20  import java.io.BufferedReader;
21  import java.io.IOException;
22  import java.io.InputStreamReader;
23  import java.io.InterruptedIOException;
24  import java.io.OutputStreamWriter;
25  import java.io.PrintWriter;
26  import java.net.InetAddress;
27  import java.net.SocketException;
28  
29  import org.apache.commons.net.echo.EchoTCPClient;
30  import org.apache.commons.net.echo.EchoUDPClient;
31  
32  /**
33   * This is an example program demonstrating how to use the EchoTCPClient
34   * and EchoUDPClient classes.  This program connects to the default echo
35   * service port of a specified server, then reads lines from standard
36   * input, writing them to the echo server, and then printing the echo.
37   * The default is to use the TCP port.  Use the -udp flag to use the UDP
38   * port.
39   * <p>
40   * Usage: echo [-udp] <hostname>
41   */
42  public final class echo
43  {
44  
45      public static void echoTCP(final String host) throws IOException
46      {
47          final EchoTCPClientchoTCPClient.html#EchoTCPClient">EchoTCPClient client = new EchoTCPClient();
48          final BufferedReader input;
49          final BufferedReader echoInput;
50          final PrintWriter echoOutput;
51          String line;
52  
53          // We want to timeout if a response takes longer than 60 seconds
54          client.setDefaultTimeout(60000);
55          client.connect(host);
56          System.out.println("Connected to " + host + ".");
57          input = new BufferedReader(new InputStreamReader(System.in));
58          echoOutput =
59              new PrintWriter(new OutputStreamWriter(client.getOutputStream()), true);
60          echoInput =
61              new BufferedReader(new InputStreamReader(client.getInputStream()));
62  
63          while ((line = input.readLine()) != null)
64          {
65              echoOutput.println(line);
66              System.out.println(echoInput.readLine());
67          }
68          echoOutput.close();
69          echoInput.close();
70          echoInput.close();
71          client.disconnect();
72      }
73  
74      public static void echoUDP(final String host) throws IOException
75      {
76          int length, count;
77          byte[] data;
78          String line;
79          final BufferedReader input;
80          final InetAddress address;
81          final EchoUDPClient client;
82  
83          input = new BufferedReader(new InputStreamReader(System.in));
84          address = InetAddress.getByName(host);
85          client = new EchoUDPClient();
86  
87          client.open();
88          // If we don't receive an echo within 5 seconds, assume the packet is lost.
89          client.setSoTimeout(5000);
90          System.out.println("Ready to echo to " + host + ".");
91  
92          // Remember, there are no guarantees about the ordering of returned
93          // UDP packets, so there is a chance the output may be jumbled.
94          while ((line = input.readLine()) != null)
95          {
96              data = line.getBytes();
97              client.send(data, address);
98              count = 0;
99              do
100             {
101                 try
102                 {
103                     length = client.receive(data);
104                 }
105                 // Here we catch both SocketException and InterruptedIOException,
106                 // because even though the JDK 1.1 docs claim that
107                 // InterruptedIOException is thrown on a timeout, it seems
108                 // SocketException is also thrown.
109                 catch (final SocketException e)
110                 {
111                     // We timed out and assume the packet is lost.
112                     System.err.println(
113                         "SocketException: Timed out and dropped packet");
114                     break;
115                 }
116                 catch (final InterruptedIOException e)
117                 {
118                     // We timed out and assume the packet is lost.
119                     System.err.println(
120                         "InterruptedIOException: Timed out and dropped packet");
121                     break;
122                 }
123                 System.out.print(new String(data, 0, length));
124                 count += length;
125             }
126             while (count < data.length);
127 
128             System.out.println();
129         }
130 
131         client.close();
132     }
133 
134 
135     public static void main(final String[] args)
136     {
137 
138         if (args.length == 1)
139         {
140             try
141             {
142                 echoTCP(args[0]);
143             }
144             catch (final IOException e)
145             {
146                 e.printStackTrace();
147                 System.exit(1);
148             }
149         }
150         else if (args.length == 2 && args[0].equals("-udp"))
151         {
152             try
153             {
154                 echoUDP(args[1]);
155             }
156             catch (final IOException e)
157             {
158                 e.printStackTrace();
159                 System.exit(1);
160             }
161         }
162         else
163         {
164             System.err.println("Usage: echo [-udp] <hostname>");
165             System.exit(1);
166         }
167 
168     }
169 
170 }
171