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 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 final void echoTCP(String host) throws IOException
46      {
47          EchoTCPClient client = new EchoTCPClient();
48          BufferedReader input, echoInput;
49          PrintWriter echoOutput;
50          String line;
51  
52          // We want to timeout if a response takes longer than 60 seconds
53          client.setDefaultTimeout(60000);
54          client.connect(host);
55          System.out.println("Connected to " + host + ".");
56          input = new BufferedReader(new InputStreamReader(System.in));
57          echoOutput =
58              new PrintWriter(new OutputStreamWriter(client.getOutputStream()), true);
59          echoInput =
60              new BufferedReader(new InputStreamReader(client.getInputStream()));
61  
62          while ((line = input.readLine()) != null)
63          {
64              echoOutput.println(line);
65              System.out.println(echoInput.readLine());
66          }
67  
68          client.disconnect();
69      }
70  
71      public static final void echoUDP(String host) throws IOException
72      {
73          int length, count;
74          byte[] data;
75          String line;
76          BufferedReader input;
77          InetAddress address;
78          EchoUDPClient client;
79  
80          input = new BufferedReader(new InputStreamReader(System.in));
81          address = InetAddress.getByName(host);
82          client = new EchoUDPClient();
83  
84          client.open();
85          // If we don't receive an echo within 5 seconds, assume the packet is lost.
86          client.setSoTimeout(5000);
87          System.out.println("Ready to echo to " + host + ".");
88  
89          // Remember, there are no guarantees about the ordering of returned
90          // UDP packets, so there is a chance the output may be jumbled.
91          while ((line = input.readLine()) != null)
92          {
93              data = line.getBytes();
94              client.send(data, address);
95              count = 0;
96              do
97              {
98                  try
99                  {
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