Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package weathertelnet;
- import java.io.BufferedReader;
- import static java.lang.System.out;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.InetAddress;
- import java.net.SocketException;
- import java.util.concurrent.ConcurrentLinkedQueue;
- import java.util.logging.Logger;
- import org.apache.commons.net.telnet.TelnetClient;
- public final class Telnet {
- private final static Logger LOG = Logger.getLogger(Telnet.class.getName());
- private TelnetClient telnetClient = new TelnetClient();
- public Telnet() throws SocketException, IOException {
- InetAddress host = InetAddress.getByName("rainmaker.wunderground.com");
- int port = 3000;
- telnetClient.connect(host, port);
- final InputStream inputStream = telnetClient.getInputStream();
- final ConcurrentLinkedQueue<Character> clq = new ConcurrentLinkedQueue();
- final StringBuilder sb = new StringBuilder();
- Thread print = new Thread() {
- @Override
- public void run() {
- out.println("print..");
- try {
- char ch = (char) inputStream.read();
- while (255 > ch && ch >= 0) {
- clq.add(ch);
- out.print(ch);
- ch = (char) inputStream.read();
- }
- } catch (IOException ex) {
- out.println("cannot read inputStream:\t" + ex);
- }
- }
- };
- Thread read = new Thread() {
- @Override
- public void run() {
- BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- try {
- do {
- String command = in.readLine();
- sendCommand(command);
- } while (true);
- } catch (IOException ex) {
- }
- }
- };
- print.start();
- read.start();
- }
- private void sendCommand(String command) throws IOException {
- byte[] bytes = command.getBytes();
- out.println("sending bytes...\t\t" + command);
- byte by = 0;
- for (int i = 0; i < bytes.length; i++) {
- by = bytes[i];
- telnetClient.sendCommand(by);
- out.println("sent byte\t" + by);
- }
- by = 10; //send newline
- telnetClient.sendCommand(by);
- }
- public static void main(String[] args) throws SocketException, IOException {
- new Telnet();
- }
- }
- execution:
- thufir@dur:~$
- thufir@dur:~$ java -jar NetBeansProjects/SSCCE/
- build/ dist/ nbproject/ src/
- thufir@dur:~$ java -jar NetBeansProjects/SSCCE/dist/
- lib/ SSCCE.jar
- thufir@dur:~$ java -jar NetBeansProjects/SSCCE/dist/SSCCE.jar
- print..
- ------------------------------------------------------------------------------
- * Welcome to THE WEATHER UNDERGROUND telnet service! *
- ------------------------------------------------------------------------------
- * *
- * National Weather Service information provided by Alden Electronics, Inc. *
- * and updated each minute as reports come in over our data feed. *
- * *
- * **Note: If you cannot get past this opening screen, you must use a *
- * different version of the "telnet" program--some of the ones for IBM *
- * compatible PC's have a bug that prevents proper connection. *
- * *
- * comments: [email protected] *
- ------------------------------------------------------------------------------
- Press Return to continue:fdk
- sending bytes... fdk
- sent byte 102
- sent byte 100
- sent byte 107
- Press Return for menu
- or enter 3 letter forecast city code-- dca
- sending bytes... dca
- sent byte 100
- sent byte 99
- sent byte 97
- No climatic data available.
- /wx/data/DAT/CLI/CLI
- CITY FORECAST MENU
- ---------------------------------------------------
- 1) Print forecast for selected city
- 2) Print climatic data for selected city
- 3) Display 3-letter city codes for a selected state
- 4) Display all 2-letter state codes
- M) Return to main menu
- X) Exit program
- ?) Help
- Selection:1
- sending bytes... 1
- sent byte 49
- No climatic data available.
- /wx/data/DAT/DAT
- CITY FORECAST MENU
- ---------------------------------------------------
- 1) Print forecast for selected city
- 2) Print climatic data for selected city
- 3) Display 3-letter city codes for a selected state
- 4) Display all 2-letter state codes
- M) Return to main menu
- X) Exit program
- ?) Help
- Selection:^Cthufir@dur:~$
- thufir@dur:~$
- thufir@dur:~$
Advertisement
Add Comment
Please, Sign In to add comment