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();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment