1 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.net.InetAddress ; 23 import java.net.SocketException ; 24 import org.apache.commons.net.CharGenTCPClient; 25 import org.apache.commons.net.CharGenUDPClient; 26 27 40 public final class chargen 41 { 42 43 public static final void chargenTCP(String host) throws IOException 44 { 45 int lines = 100; 46 String line; 47 CharGenTCPClient client = new CharGenTCPClient(); 48 BufferedReader chargenInput; 49 50 client.setDefaultTimeout(60000); 52 client.connect(host); 53 chargenInput = 54 new BufferedReader (new InputStreamReader (client.getInputStream())); 55 56 while (lines-- > 0) 60 { 61 if ((line = chargenInput.readLine()) == null) 62 break; 63 System.out.println(line); 64 } 65 66 client.disconnect(); 67 } 68 69 public static final void chargenUDP(String host) throws IOException 70 { 71 int packets = 50; 72 byte[] data; 73 InetAddress address; 74 CharGenUDPClient client; 75 76 address = InetAddress.getByName(host); 77 client = new CharGenUDPClient(); 78 79 client.open(); 80 client.setSoTimeout(5000); 83 84 while (packets-- > 0) 85 { 86 client.send(address); 87 88 try 89 { 90 data = client.receive(); 91 } 92 catch (SocketException e) 97 { 98 System.err.println("SocketException: Timed out and dropped packet"); 100 continue; 101 } 102 catch (InterruptedIOException e) 103 { 104 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 154 | Popular Tags |