| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| 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; |
|
| 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 | 0 | public final class echo |
| 41 | { |
|
| 42 | ||
| 43 | public static final void echoTCP(String host) throws IOException |
|
| 44 | { |
|
| 45 | 0 | EchoTCPClient client = new EchoTCPClient(); |
| 46 | BufferedReader input, echoInput; |
|
| 47 | PrintWriter echoOutput; |
|
| 48 | String line; |
|
| 49 | ||
| 50 | // We want to timeout if a response takes longer than 60 seconds |
|
| 51 | 0 | client.setDefaultTimeout(60000); |
| 52 | 0 | client.connect(host); |
| 53 | 0 | System.out.println("Connected to " + host + "."); |
| 54 | 0 | input = new BufferedReader(class="keyword">new InputStreamReader(System.in)); |
| 55 | 0 | echoOutput = |
| 56 | new PrintWriter(class="keyword">new OutputStreamWriter(client.getOutputStream()), true); |
|
| 57 | 0 | echoInput = |
| 58 | new BufferedReader(class="keyword">new InputStreamReader(client.getInputStream())); |
|
| 59 | ||
| 60 | 0 | while ((line = input.readLine()) != null) |
| 61 | { |
|
| 62 | 0 | echoOutput.println(line); |
| 63 | 0 | System.out.println(echoInput.readLine()); |
| 64 | } |
|
| 65 | ||
| 66 | 0 | client.disconnect(); |
| 67 | 0 | } |
| 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 | 0 | input = new BufferedReader(class="keyword">new InputStreamReader(System.in)); |
| 79 | 0 | address = InetAddress.getByName(host); |
| 80 | 0 | client = new EchoUDPClient(); |
| 81 | ||
| 82 | 0 | client.open(); |
| 83 | // If we don't receive an echo within 5 seconds, assume the packet is lost. |
|
| 84 | 0 | client.setSoTimeout(5000); |
| 85 | 0 | 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 | 0 | while ((line = input.readLine()) != null) |
| 90 | { |
|
| 91 | 0 | data = line.getBytes(); |
| 92 | 0 | client.send(data, address); |
| 93 | 0 | count = 0; |
| 94 | do |
|
| 95 | { |
|
| 96 | try |
|
| 97 | { |
|
| 98 | 0 | 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 | 0 | catch (SocketException e) |
| 105 | { |
|
| 106 | // We timed out and assume the packet is lost. |
|
| 107 | 0 | System.err.println( |
| 108 | "SocketException: Timed out and dropped packet"); |
|
| 109 | 0 | break; |
| 110 | } |
|
| 111 | 0 | catch (InterruptedIOException e) |
| 112 | { |
|
| 113 | // We timed out and assume the packet is lost. |
|
| 114 | 0 | System.err.println( |
| 115 | "InterruptedIOException: Timed out and dropped packet"); |
|
| 116 | 0 | break; |
| 117 | 0 | } |
| 118 | 0 | System.out.print(new String(data, 0, length)); |
| 119 | 0 | count += length; |
| 120 | } |
|
| 121 | 0 | while (count < data.length); |
| 122 | ||
| 123 | 0 | System.out.println(); |
| 124 | } |
|
| 125 | ||
| 126 | 0 | client.close(); |
| 127 | 0 | } |
| 128 | ||
| 129 | ||
| 130 | public static final void main(String[] args) |
|
| 131 | { |
|
| 132 | ||
| 133 | 0 | if (args.length == 1) |
| 134 | { |
|
| 135 | try |
|
| 136 | { |
|
| 137 | 0 | echoTCP(args[0]); |
| 138 | } |
|
| 139 | 0 | catch (IOException e) |
| 140 | { |
|
| 141 | 0 | e.printStackTrace(); |
| 142 | 0 | System.exit(1); |
| 143 | 0 | } |
| 144 | } |
|
| 145 | 0 | else if (args.length == 2 && args[0].equals("-udp")) |
| 146 | { |
|
| 147 | try |
|
| 148 | { |
|
| 149 | 0 | echoUDP(args[1]); |
| 150 | } |
|
| 151 | 0 | catch (IOException e) |
| 152 | { |
|
| 153 | 0 | e.printStackTrace(); |
| 154 | 0 | System.exit(1); |
| 155 | 0 | } |
| 156 | } |
|
| 157 | else |
|
| 158 | { |
|
| 159 | 0 | System.err.println("Usage: echo [-udp] <hostname>"); |
| 160 | 0 | System.exit(1); |
| 161 | } |
|
| 162 | ||
| 163 | 0 | } |
| 164 | ||
| 165 | } |
|
| 166 |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |