thufir

SSCCE sendCommand needs to send a newline, 10, as a byte

Sep 1st, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.25 KB | None | 0 0
  1. package weathertelnet;
  2.  
  3. import java.io.BufferedReader;
  4. import static java.lang.System.out;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.net.InetAddress;
  9. import java.net.SocketException;
  10. import java.util.concurrent.ConcurrentLinkedQueue;
  11. import java.util.logging.Logger;
  12. import org.apache.commons.net.telnet.TelnetClient;
  13.  
  14. public final class Telnet {
  15.  
  16.     private final static Logger LOG = Logger.getLogger(Telnet.class.getName());
  17.     private TelnetClient telnetClient = new TelnetClient();
  18.  
  19.     public Telnet() throws SocketException, IOException {
  20.         InetAddress host = InetAddress.getByName("rainmaker.wunderground.com");
  21.         int port = 3000;
  22.         telnetClient.connect(host, port);
  23.  
  24.         final InputStream inputStream = telnetClient.getInputStream();
  25.         final ConcurrentLinkedQueue<Character> clq = new ConcurrentLinkedQueue();
  26.         final StringBuilder sb = new StringBuilder();
  27.  
  28.         Thread print = new Thread() {
  29.  
  30.             @Override
  31.             public void run() {
  32.                 out.println("print..");
  33.                 try {
  34.                     char ch = (char) inputStream.read();
  35.                     while (255 > ch && ch >= 0) {
  36.                         clq.add(ch);
  37.                         out.print(ch);
  38.                         ch = (char) inputStream.read();
  39.                     }
  40.                 } catch (IOException ex) {
  41.                     out.println("cannot read inputStream:\t" + ex);
  42.                 }
  43.             }
  44.         };
  45.  
  46.  
  47.         Thread read = new Thread() {
  48.  
  49.             @Override
  50.             public void run() {
  51.                 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  52.                 try {
  53.                     do {
  54.                         String command = in.readLine();
  55.                         sendCommand(command);
  56.                     } while (true);
  57.                 } catch (IOException ex) {
  58.                 }
  59.             }
  60.         };
  61.         print.start();
  62.         read.start();
  63.     }
  64.  
  65.     private void sendCommand(String command) throws IOException {
  66.         byte[] bytes = command.getBytes();
  67.         out.println("sending bytes...\t\t" + command);
  68.         byte by = 0;
  69.         for (int i = 0; i < bytes.length; i++) {
  70.             by = bytes[i];
  71.             telnetClient.sendCommand(by);
  72.             out.println("sent byte\t" + by);
  73.         }
  74.         by = 10;  //send newline
  75.         telnetClient.sendCommand(by);
  76.     }
  77.  
  78.     public static void main(String[] args) throws SocketException, IOException {
  79.         new Telnet();
  80.     }
  81. }
  82.  
  83.  
  84.  
  85.  
  86. execution:
  87.  
  88.  
  89. thufir@dur:~$
  90. thufir@dur:~$ java -jar NetBeansProjects/SSCCE/
  91. build/     dist/      nbproject/ src/      
  92. thufir@dur:~$ java -jar NetBeansProjects/SSCCE/dist/
  93. lib/       SSCCE.jar  
  94. thufir@dur:~$ java -jar NetBeansProjects/SSCCE/dist/SSCCE.jar
  95. print..
  96. ------------------------------------------------------------------------------
  97. *               Welcome to THE WEATHER UNDERGROUND telnet service!            *
  98. ------------------------------------------------------------------------------
  99. *                                                                            *
  100. *   National Weather Service information provided by Alden Electronics, Inc. *
  101. *    and updated each minute as reports come in over our data feed.          *
  102. *                                                                            *
  103. *   **Note: If you cannot get past this opening screen, you must use a       *
  104. *   different version of the "telnet" program--some of the ones for IBM      *
  105. *   compatible PC's have a bug that prevents proper connection.              *
  106. *                                                                            *
  107. *           comments: [email protected]                              *
  108. ------------------------------------------------------------------------------
  109.  
  110. Press Return to continue:fdk
  111. sending bytes...        fdk
  112. sent byte   102
  113. sent byte   100
  114. sent byte   107
  115.  
  116. Press Return for menu
  117. or enter 3 letter forecast city code-- dca
  118. sending bytes...        dca
  119. sent byte   100
  120. sent byte   99
  121. sent byte   97
  122.  
  123.     No climatic data available.
  124. /wx/data/DAT/CLI/CLI
  125.                         CITY FORECAST MENU
  126.                ---------------------------------------------------
  127.                1) Print forecast for selected city
  128.                2) Print climatic data for selected city
  129.                3) Display 3-letter city codes for a selected state
  130.                4) Display all 2-letter state codes
  131.                M) Return to main menu
  132.                X) Exit program
  133.                ?) Help
  134.                   Selection:1
  135. sending bytes...        1
  136. sent byte   49
  137.  
  138.     No climatic data available.
  139. /wx/data/DAT/DAT
  140.                         CITY FORECAST MENU
  141.                ---------------------------------------------------
  142.                1) Print forecast for selected city
  143.                2) Print climatic data for selected city
  144.                3) Display 3-letter city codes for a selected state
  145.                4) Display all 2-letter state codes
  146.                M) Return to main menu
  147.                X) Exit program
  148.                ?) Help
  149.                   Selection:^Cthufir@dur:~$
  150. thufir@dur:~$
  151. thufir@dur:~$
Advertisement
Add Comment
Please, Sign In to add comment