thufir

seems to wait for data correctly

Aug 31st, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. thufir@dur:~$
  2. thufir@dur:~$ java -jar NetBeansProjects/Console/dist/Console.jar
  3. connecting...
  4. print..
  5. makeString..
  6. java.util.NoSuchElementException
  7. ------------------------------------------------------------------------------
  8. * Welcome to THE WEATHER UNDERGROUND telnet service! *
  9. ------------------------------------------------------------------------------
  10. * *
  11. * National Weather Service information provided by Alden Electronics, Inc. *
  12. * and updated each minute as reports come in over our data feed. *
  13. * *
  14. * **Note: If you cannot get past this opening screen, you must use a *
  15. * different version of the "telnet" program--some of the ones for IBM *
  16. * compatible PC's have a bug that prevents proper connection. *
  17. * *
  18. * comments: [email protected] *
  19. ------------------------------------------------------------------------------
  20.  
  21. Press Return to continue:finally -- waiting for more data..
  22. java.lang.ClassCastException: java.lang.Character cannot be cast to java.lang.Byte
  23. finally -- waiting for more data..
  24. java.lang.ClassCastException: java.lang.Character cannot be cast to java.lang.Byte
  25. finally -- waiting for more data..
  26. java.lang.ClassCastException: java.lang.Character cannot be cast to java.lang.Byte
  27. finally -- waiting for more data..
  28. java.lang.ClassCastException: java.lang.Character cannot be cast to java.lang.Byte
  29. finally -- waiting for more data..
  30. java.lang.ClassCastException: java.lang.Character cannot be cast to java.lang.Byte
  31. ^Cthufir@dur:~$
  32. thufir@dur:~$
  33. thufir@dur:~$ cat NetBeansProjects/Console/src/weathertelnet/TelnetWrapper.java
  34. package weathertelnet;
  35.  
  36. import static java.lang.System.out;
  37. import java.io.IOException;
  38. import java.io.InputStream;
  39. import java.net.InetAddress;
  40. import java.net.SocketException;
  41. import java.util.concurrent.ConcurrentLinkedQueue;
  42. import java.util.logging.Logger;
  43. import org.apache.commons.net.telnet.TelnetClient;
  44.  
  45. public class TelnetWrapper {
  46.  
  47. private final static Logger LOG = Logger.getLogger(TelnetWrapper.class.getName());
  48. private TelnetClient telnetClient = new TelnetClient();
  49.  
  50. TelnetWrapper(InetAddress host, int port) throws SocketException, IOException {
  51. out.println("connecting...");
  52. telnetClient.connect(host, port);
  53. consoleOutput();
  54.  
  55. }
  56.  
  57. private void consoleOutput() throws SocketException, IOException {
  58. final InputStream inputStream = telnetClient.getInputStream();
  59. final ConcurrentLinkedQueue cq = new ConcurrentLinkedQueue();
  60.  
  61. Thread print = new Thread() {
  62.  
  63. @Override
  64. public void run() {
  65. out.println("print..");
  66. try {
  67. char ch = (char) inputStream.read();
  68. while (255 > ch && ch >= 0) {
  69. cq.add(ch);
  70. out.print(ch);
  71. ch = (char) inputStream.read();
  72. }
  73. } catch (IOException ex) {
  74. }
  75. }
  76. };
  77.  
  78. Thread makeString = new Thread() {//hmm, takes no arguments
  79.  
  80. @Override
  81. public void run() {
  82. out.println("makeString..");
  83. do {
  84. try {
  85. byte b = (byte) cq.remove();
  86. char ch = (char) b;
  87. } catch (java.util.NoSuchElementException | ClassCastException e) {
  88. out.println(e);
  89. try {
  90. Thread.sleep(1000);
  91. } catch (InterruptedException ce) {
  92. }
  93. } finally {
  94. out.println("finally -- waiting for more data..");
  95. try {
  96. Thread.sleep(1000);
  97. } catch (InterruptedException ex) {
  98. }
  99. }
  100. } while (true);
  101. }
  102. };
  103. print.start();
  104. makeString.start();
  105. }
  106.  
  107. private void cmd(String cmd) throws IOException {//haven't tested yet..
  108. byte[] b = cmd.getBytes();
  109. System.out.println("streamreader has\t\t" + cmd);
  110. int l = b.length;
  111. for (int i = 0; i < l; i++) {
  112. telnetClient.sendCommand(b[i]);
  113. }
  114. }
  115. }thufir@dur:~$
  116. thufir@dur:~$
Advertisement
Add Comment
Please, Sign In to add comment