001 /* 002 * Copyright 2001-2005 The Apache Software Foundation 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package examples; 017 018 import java.io.BufferedReader; 019 import java.io.IOException; 020 import java.io.InputStreamReader; 021 import java.io.InterruptedIOException; 022 import java.net.InetAddress; 023 import java.net.SocketException; 024 import org.apache.commons.net.CharGenTCPClient; 025 import org.apache.commons.net.CharGenUDPClient; 026 027 /*** 028 * This is an example program demonstrating how to use the CharGenTCPClient 029 * and CharGenUDPClient classes. This program connects to the default 030 * chargen service port of a specified server, then reads 100 lines from 031 * of generated output, writing each line to standard output, and then 032 * closes the connection. The UDP invocation of the program sends 50 033 * datagrams, printing the reply to each. 034 * The default is to use the TCP port. Use the -udp flag to use the UDP 035 * port. 036 * <p> 037 * Usage: chargen [-udp] <hostname> 038 * <p> 039 ***/ 040 public final class chargen 041 { 042 043 public static final void chargenTCP(String host) throws IOException 044 { 045 int lines = 100; 046 String line; 047 CharGenTCPClient client = new CharGenTCPClient(); 048 BufferedReader chargenInput; 049 050 // We want to timeout if a response takes longer than 60 seconds 051 client.setDefaultTimeout(60000); 052 client.connect(host); 053 chargenInput = 054 new BufferedReader(new InputStreamReader(client.getInputStream())); 055 056 // We assume the chargen service outputs lines, but it really doesn't 057 // have to, so this code might actually not work if no newlines are 058 // present. 059 while (lines-- > 0) 060 { 061 if ((line = chargenInput.readLine()) == null) 062 break; 063 System.out.println(line); 064 } 065 066 client.disconnect(); 067 } 068 069 public static final void chargenUDP(String host) throws IOException 070 { 071 int packets = 50; 072 byte[] data; 073 InetAddress address; 074 CharGenUDPClient client; 075 076 address = InetAddress.getByName(host); 077 client = new CharGenUDPClient(); 078 079 client.open(); 080 // If we don't receive a return packet within 5 seconds, assume 081 // the packet is lost. 082 client.setSoTimeout(5000); 083 084 while (packets-- > 0) 085 { 086 client.send(address); 087 088 try 089 { 090 data = client.receive(); 091 } 092 // Here we catch both SocketException and InterruptedIOException, 093 // because even though the JDK 1.1 docs claim that 094 // InterruptedIOException is thrown on a timeout, it seems 095 // SocketException is also thrown. 096 catch (SocketException e) 097 { 098 // We timed out and assume the packet is lost. 099 System.err.println("SocketException: Timed out and dropped packet"); 100 continue; 101 } 102 catch (InterruptedIOException e) 103 { 104 // We timed out and assume the packet is lost. 105 System.err.println( 106 "InterruptedIOException: Timed out and dropped packet"); 107 continue; 108 } 109 System.out.write(data); 110 System.out.flush(); 111 } 112 113 client.close(); 114 } 115 116 117 public static final void main(String[] args) 118 { 119 120 if (args.length == 1) 121 { 122 try 123 { 124 chargenTCP(args[0]); 125 } 126 catch (IOException e) 127 { 128 e.printStackTrace(); 129 System.exit(1); 130 } 131 } 132 else if (args.length == 2 && args[0].equals("-udp")) 133 { 134 try 135 { 136 chargenUDP(args[1]); 137 } 138 catch (IOException e) 139 { 140 e.printStackTrace(); 141 System.exit(1); 142 } 143 } 144 else 145 { 146 System.err.println("Usage: chargen [-udp] <hostname>"); 147 System.exit(1); 148 } 149 150 } 151 152 } 153