1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 import java.nio.charset.Charset;
29 import java.time.Duration;
30
31 import org.apache.commons.net.echo.EchoTCPClient;
32 import org.apache.commons.net.echo.EchoUDPClient;
33
34
35
36
37
38
39
40
41
42 public final class echo {
43
44 public static void echoTCP(final String host) throws IOException {
45 final EchoTCPClient client = new EchoTCPClient();
46 String line;
47
48
49 client.setDefaultTimeout(60000);
50 client.connect(host);
51 try {
52 System.out.println("Connected to " + host + ".");
53 final Charset charset = Charset.defaultCharset();
54 final BufferedReader input = new BufferedReader(new InputStreamReader(System.in, charset));
55 try (PrintWriter echoOutput = new PrintWriter(new OutputStreamWriter(client.getOutputStream(), charset), true);
56 BufferedReader echoInput = new BufferedReader(new InputStreamReader(client.getInputStream(), charset))) {
57 while ((line = input.readLine()) != null) {
58 echoOutput.println(line);
59 System.out.println(echoInput.readLine());
60 }
61 }
62
63 } finally {
64 client.disconnect();
65 }
66 }
67
68 public static void echoUDP(final String host) throws IOException {
69 int length, count;
70 byte[] data;
71 String line;
72 final BufferedReader input;
73 final InetAddress address;
74
75 final Charset charset = Charset.defaultCharset();
76 input = new BufferedReader(new InputStreamReader(System.in, charset));
77 address = InetAddress.getByName(host);
78 try (EchoUDPClient client = new EchoUDPClient()) {
79
80 client.open();
81
82 client.setSoTimeout(Duration.ofSeconds(5));
83 System.out.println("Ready to echo to " + host + ".");
84
85
86
87 while ((line = input.readLine()) != null) {
88 data = line.getBytes(charset);
89 client.send(data, address);
90 count = 0;
91 do {
92 try {
93 length = client.receive(data);
94 }
95
96
97
98
99 catch (final SocketException e) {
100
101 System.err.println("SocketException: Timed out and dropped packet");
102 break;
103 } catch (final InterruptedIOException e) {
104
105 System.err.println("InterruptedIOException: Timed out and dropped packet");
106 break;
107 }
108 System.out.print(new String(data, 0, length, charset));
109 count += length;
110 } while (count < data.length);
111
112 System.out.println();
113 }
114
115 }
116 }
117
118 public static void main(final String[] args) {
119
120 if (args.length == 1) {
121 try {
122 echoTCP(args[0]);
123 } catch (final IOException e) {
124 e.printStackTrace();
125 System.exit(1);
126 }
127 } else if (args.length == 2 && args[0].equals("-udp")) {
128 try {
129 echoUDP(args[1]);
130 } catch (final IOException e) {
131 e.printStackTrace();
132 System.exit(1);
133 }
134 } else {
135 System.err.println("Usage: echo [-udp] <hostname>");
136 System.exit(1);
137 }
138
139 }
140
141 }