thufir

SSCCE trying to send byte commands through telnet

Sep 1st, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 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[] b = command.getBytes();
  67.         out.println("sending bytes...\t\t" + command);
  68.         int l = b.length;
  69.         for (int i = 0; i < l; i++) {
  70.             telnetClient.sendCommand(b[i]);
  71.             out.println("byte\t" + b);//hmm, not the output I expect, like 10, 22, an int for char value
  72.         }
  73.         out.println("\n...sent bytes\n");
  74.     }
  75.  
  76.     public static void main(String[] args) throws SocketException, IOException {
  77.         new Telnet();
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment