Writing a telnet client in java - how to listen for new data?

19,283

I've been holding off on answering because there are a couple different things going on here -- Google doesn't have a telnet server running on port 80, it's a web (HTTP) server. You're connecting to the webserver with your telnet client and trying to talk over HTTP with plain text. HTTP and telnet are two different protocols.

So there is a mismatch between what you want to do and what these Java telnet clients are designed to do -- namely connect to a remote shell.

Based on what you've been saying, I think you can get it done much more easily by just making an HTTP request. There's a dead simple solution in this answer:

import java.net.*;
import java.io.*;

public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL yahoo = new URL("http://www.yahoo.com/");
        URLConnection yc = yahoo.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                yc.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}

Sockets are complicated stuff. Take a look at this tutorial if you want to dig deeper in to them. But they're going to be a huge pain to work with and probably not help you get what you need done.

Share:
19,283
matt
Author by

matt

Updated on June 05, 2022

Comments

  • matt
    matt almost 2 years

    I'm attempting to implement a Telnet client in Java, which will just get data from a telnet server.

    I've been looking for ages now and have found a lot of things, but not particularly what I need - for example the apache commons client example, which seems to send a lot of commands, which is just confusing me to be honest. So I therefore thought it would be easier to just to write my own client which connects to the server using a socket.

     public class TelnetClient {
        private String host;
    
        public TelnetClient(String host) {
            this.host = host;
        }
    
        public void getData(){
            Socket s = new Socket();
            PrintWriter s_out = null;
            BufferedReader s_in = null;
    
            try {
                s.connect(new InetSocketAddress("www.google.com" , 80));
                System.out.println("Connected");
    
                //writer for socket
                s_out = new PrintWriter( s.getOutputStream(), true);
                //reader for socket
                s_in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            }
    
            //Host not found
            catch (UnknownHostException e) {
                System.err.println("Don't know about host : " + host);
                System.exit(1);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            //Send message to server
            String message = "GET / HTTP/1.1\r\n\r\n";
            s_out.println( message );
    
            System.out.println("Message send");
    
            //Get response from server
            String response;
            try {
                while ((response = s_in.readLine()) != null) {
                    System.out.println( response );
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    But I cant actually test this with the server currently so Im just using google.com, however I want to change it to listen continuously for new lines of data on the server.

    Basically my question is, am I going about this the wrong way - am I being naive by just using sockets to access the telnet server and am I underestimating what a telnet client/server should be, thanks for any help.

    Also if anyone has any good/simple examples of a telnet client, that would be very useful!!