KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > echo


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

16 package examples;
17
18 import java.io.BufferedReader JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStreamReader JavaDoc;
21 import java.io.InterruptedIOException JavaDoc;
22 import java.io.OutputStreamWriter JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.net.InetAddress JavaDoc;
25 import java.net.SocketException JavaDoc;
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 JavaDoc host) throws IOException JavaDoc
44     {
45         EchoTCPClient client = new EchoTCPClient();
46         BufferedReader JavaDoc input, echoInput;
47         PrintWriter JavaDoc echoOutput;
48         String JavaDoc line;
49
50         // We want to timeout if a response takes longer than 60 seconds
51
client.setDefaultTimeout(60000);
52         client.connect(host);
53         System.out.println("Connected to " + host + ".");
54         input = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
55         echoOutput =
56             new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(client.getOutputStream()), true);
57         echoInput =
58             new BufferedReader JavaDoc(new InputStreamReader JavaDoc(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 JavaDoc host) throws IOException JavaDoc
70     {
71         int length, count;
72         byte[] data;
73         String JavaDoc line;
74         BufferedReader JavaDoc input;
75         InetAddress JavaDoc address;
76         EchoUDPClient client;
77
78         input = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
79         address = InetAddress.getByName(host);
80         client = new EchoUDPClient();
81
82         client.open();
83         // If we don't receive an echo within 5 seconds, assume the packet is lost.
84
client.setSoTimeout(5000);
85         System.out.println("Ready to echo to " + host + ".");
86
87         // Remember, there are no guarantees about the ordering of returned
88
// UDP packets, so there is a chance the output may be jumbled.
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                 // Here we catch both SocketException and InterruptedIOException,
101
// because even though the JDK 1.1 docs claim that
102
// InterruptedIOException is thrown on a timeout, it seems
103
// SocketException is also thrown.
104
catch (SocketException JavaDoc e)
105                 {
106                     // We timed out and assume the packet is lost.
107
System.err.println(
108                         "SocketException: Timed out and dropped packet");
109                     break;
110                 }
111                 catch (InterruptedIOException JavaDoc e)
112                 {
113                     // We timed out and assume the packet is lost.
114
System.err.println(
115                         "InterruptedIOException: Timed out and dropped packet");
116                     break;
117                 }
118                 System.out.print(new String JavaDoc(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 JavaDoc[] args)
131     {
132
133         if (args.length == 1)
134         {
135             try
136             {
137                 echoTCP(args[0]);
138             }
139             catch (IOException JavaDoc 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 JavaDoc 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
167
Popular Tags