View Javadoc
1 package examples; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Commons" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.io.BufferedReader; 58 import java.io.IOException; 59 import java.io.InputStreamReader; 60 import java.io.InterruptedIOException; 61 import java.io.OutputStreamWriter; 62 import java.io.PrintWriter; 63 import java.net.InetAddress; 64 import java.net.SocketException; 65 import org.apache.commons.net.EchoTCPClient; 66 import org.apache.commons.net.EchoUDPClient; 67 68 /**** 69 * This is an example program demonstrating how to use the EchoTCPClient 70 * and EchoUDPClient classes. This program connects to the default echo 71 * service port of a specified server, then reads lines from standard 72 * input, writing them to the echo server, and then printing the echo. 73 * The default is to use the TCP port. Use the -udp flag to use the UDP 74 * port. 75 * <p> 76 * Usage: echo [-udp] <hostname> 77 * <p> 78 ***/ 79 public final class echo 80 { 81 82 public static final void echoTCP(String host) throws IOException 83 { 84 EchoTCPClient client = new EchoTCPClient(); 85 BufferedReader input, echoInput; 86 PrintWriter echoOutput; 87 String line; 88 89 // We want to timeout if a response takes longer than 60 seconds 90 client.setDefaultTimeout(60000); 91 client.connect(host); 92 System.out.println("Connected to " + host + "."); 93 input = new BufferedReader(new InputStreamReader(System.in)); 94 echoOutput = 95 new PrintWriter(new OutputStreamWriter(client.getOutputStream()), true); 96 echoInput = 97 new BufferedReader(new InputStreamReader(client.getInputStream())); 98 99 while ((line = input.readLine()) != null) 100 { 101 echoOutput.println(line); 102 System.out.println(echoInput.readLine()); 103 } 104 105 client.disconnect(); 106 } 107 108 public static final void echoUDP(String host) throws IOException 109 { 110 int length, count; 111 byte[] data; 112 String line; 113 BufferedReader input; 114 InetAddress address; 115 EchoUDPClient client; 116 117 input = new BufferedReader(new InputStreamReader(System.in)); 118 address = InetAddress.getByName(host); 119 client = new EchoUDPClient(); 120 121 client.open(); 122 // If we don't receive an echo within 5 seconds, assume the packet is lost. 123 client.setSoTimeout(5000); 124 System.out.println("Ready to echo to " + host + "."); 125 126 // Remember, there are no guarantees about the ordering of returned 127 // UDP packets, so there is a chance the output may be jumbled. 128 while ((line = input.readLine()) != null) 129 { 130 data = line.getBytes(); 131 client.send(data, address); 132 count = 0; 133 do 134 { 135 try 136 { 137 length = client.receive(data); 138 } 139 // Here we catch both SocketException and InterruptedIOException, 140 // because even though the JDK 1.1 docs claim that 141 // InterruptedIOException is thrown on a timeout, it seems 142 // SocketException is also thrown. 143 catch (SocketException e) 144 { 145 // We timed out and assume the packet is lost. 146 System.err.println( 147 "SocketException: Timed out and dropped packet"); 148 break; 149 } 150 catch (InterruptedIOException e) 151 { 152 // We timed out and assume the packet is lost. 153 System.err.println( 154 "InterruptedIOException: Timed out and dropped packet"); 155 break; 156 } 157 System.out.print(new String(data, 0, length)); 158 count += length; 159 } 160 while (count < data.length); 161 162 System.out.println(); 163 } 164 165 client.close(); 166 } 167 168 169 public static final void main(String[] args) 170 { 171 172 if (args.length == 1) 173 { 174 try 175 { 176 echoTCP(args[0]); 177 } 178 catch (IOException e) 179 { 180 e.printStackTrace(); 181 System.exit(1); 182 } 183 } 184 else if (args.length == 2 && args[0].equals("-udp")) 185 { 186 try 187 { 188 echoUDP(args[1]); 189 } 190 catch (IOException e) 191 { 192 e.printStackTrace(); 193 System.exit(1); 194 } 195 } 196 else 197 { 198 System.err.println("Usage: echo [-udp] <hostname>"); 199 System.exit(1); 200 } 201 202 } 203 204 } 205

This page was automatically generated by Maven