PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/android-client/lib/commons-net-3.0.1/examples/unix/chargen.java

https://bitbucket.org/SICS_projects/mp3sync
Java | 156 lines | 95 code | 20 blank | 41 comment | 10 complexity | 3b14f2ccaa24650f9055e54ffba2d795 MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package examples.unix;
  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.chargen.CharGenTCPClient;
  25. import org.apache.commons.net.chargen.CharGenUDPClient;
  26. /***
  27. * This is an example program demonstrating how to use the CharGenTCPClient
  28. * and CharGenUDPClient classes. This program connects to the default
  29. * chargen service port of a specified server, then reads 100 lines from
  30. * of generated output, writing each line to standard output, and then
  31. * closes the connection. The UDP invocation of the program sends 50
  32. * datagrams, printing the reply to each.
  33. * The default is to use the TCP port. Use the -udp flag to use the UDP
  34. * port.
  35. * <p>
  36. * Usage: chargen [-udp] <hostname>
  37. * <p>
  38. ***/
  39. public final class chargen
  40. {
  41. public static final void chargenTCP(String host) throws IOException
  42. {
  43. int lines = 100;
  44. String line;
  45. CharGenTCPClient client = new CharGenTCPClient();
  46. BufferedReader chargenInput;
  47. // We want to timeout if a response takes longer than 60 seconds
  48. client.setDefaultTimeout(60000);
  49. client.connect(host);
  50. chargenInput =
  51. new BufferedReader(new InputStreamReader(client.getInputStream()));
  52. // We assume the chargen service outputs lines, but it really doesn't
  53. // have to, so this code might actually not work if no newlines are
  54. // present.
  55. while (lines-- > 0)
  56. {
  57. if ((line = chargenInput.readLine()) == null)
  58. break;
  59. System.out.println(line);
  60. }
  61. client.disconnect();
  62. }
  63. public static final void chargenUDP(String host) throws IOException
  64. {
  65. int packets = 50;
  66. byte[] data;
  67. InetAddress address;
  68. CharGenUDPClient client;
  69. address = InetAddress.getByName(host);
  70. client = new CharGenUDPClient();
  71. client.open();
  72. // If we don't receive a return packet within 5 seconds, assume
  73. // the packet is lost.
  74. client.setSoTimeout(5000);
  75. while (packets-- > 0)
  76. {
  77. client.send(address);
  78. try
  79. {
  80. data = client.receive();
  81. }
  82. // Here we catch both SocketException and InterruptedIOException,
  83. // because even though the JDK 1.1 docs claim that
  84. // InterruptedIOException is thrown on a timeout, it seems
  85. // SocketException is also thrown.
  86. catch (SocketException e)
  87. {
  88. // We timed out and assume the packet is lost.
  89. System.err.println("SocketException: Timed out and dropped packet");
  90. continue;
  91. }
  92. catch (InterruptedIOException e)
  93. {
  94. // We timed out and assume the packet is lost.
  95. System.err.println(
  96. "InterruptedIOException: Timed out and dropped packet");
  97. continue;
  98. }
  99. System.out.write(data);
  100. System.out.flush();
  101. }
  102. client.close();
  103. }
  104. public static final void main(String[] args)
  105. {
  106. if (args.length == 1)
  107. {
  108. try
  109. {
  110. chargenTCP(args[0]);
  111. }
  112. catch (IOException e)
  113. {
  114. e.printStackTrace();
  115. System.exit(1);
  116. }
  117. }
  118. else if (args.length == 2 && args[0].equals("-udp"))
  119. {
  120. try
  121. {
  122. chargenUDP(args[1]);
  123. }
  124. catch (IOException e)
  125. {
  126. e.printStackTrace();
  127. System.exit(1);
  128. }
  129. }
  130. else
  131. {
  132. System.err.println("Usage: chargen [-udp] <hostname>");
  133. System.exit(1);
  134. }
  135. }
  136. }