1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package examples;
17
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 org.apache.commons.net.EchoTCPClient;
27 import org.apache.commons.net.EchoUDPClient;
28
29 /***
30 * This is an example program demonstrating how to use the EchoTCPClient
31 * and EchoUDPClient classes. This program connects to the default echo
32 * service port of a specified server, then reads lines from standard
33 * input, writing them to the echo server, and then printing the echo.
34 * The default is to use the TCP port. Use the -udp flag to use the UDP
35 * port.
36 * <p>
37 * Usage: echo [-udp] <hostname>
38 * <p>
39 ***/
40 public final class echo
41 {
42
43 public static final void echoTCP(String host) throws IOException
44 {
45 EchoTCPClient client = new EchoTCPClient();
46 BufferedReader input, echoInput;
47 PrintWriter echoOutput;
48 String line;
49
50
51 client.setDefaultTimeout(60000);
52 client.connect(host);
53 System.out.println("Connected to " + host + ".");
54 input = new BufferedReader(new InputStreamReader(System.in));
55 echoOutput =
56 new PrintWriter(new OutputStreamWriter(client.getOutputStream()), true);
57 echoInput =
58 new BufferedReader(new InputStreamReader(client.getInputStream()));
59
60 while ((line = input.readLine()) != null)
61 {
62 echoOutput.println(line);
63 System.out.println(echoInput.readLine());
64 }
65
66 client.disconnect();
67 }
68
69 public static final void echoUDP(String host) throws IOException
70 {
71 int length, count;
72 byte[] data;
73 String line;
74 BufferedReader input;
75 InetAddress address;
76 EchoUDPClient client;
77
78 input = new BufferedReader(new InputStreamReader(System.in));
79 address = InetAddress.getByName(host);
80 client = new EchoUDPClient();
81
82 client.open();
83
84 client.setSoTimeout(5000);
85 System.out.println("Ready to echo to " + host + ".");
86
87
88
89 while ((line = input.readLine()) != null)
90 {
91 data = line.getBytes();
92 client.send(data, address);
93 count = 0;
94 do
95 {
96 try
97 {
98 length = client.receive(data);
99 }
100
101
102
103
104 catch (SocketException e)
105 {
106
107 System.err.println(
108 "SocketException: Timed out and dropped packet");
109 break;
110 }
111 catch (InterruptedIOException e)
112 {
113
114 System.err.println(
115 "InterruptedIOException: Timed out and dropped packet");
116 break;
117 }
118 System.out.print(new String(data, 0, length));
119 count += length;
120 }
121 while (count < data.length);
122
123 System.out.println();
124 }
125
126 client.close();
127 }
128
129
130 public static final void main(String[] args)
131 {
132
133 if (args.length == 1)
134 {
135 try
136 {
137 echoTCP(args[0]);
138 }
139 catch (IOException e)
140 {
141 e.printStackTrace();
142 System.exit(1);
143 }
144 }
145 else if (args.length == 2 && args[0].equals("-udp"))
146 {
147 try
148 {
149 echoUDP(args[1]);
150 }
151 catch (IOException e)
152 {
153 e.printStackTrace();
154 System.exit(1);
155 }
156 }
157 else
158 {
159 System.err.println("Usage: echo [-udp] <hostname>");
160 System.exit(1);
161 }
162
163 }
164
165 }
166