2

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!!

Patrick Collins
  • 9,307
  • 4
  • 23
  • 60
matt
  • 79
  • 1
  • 4
  • 10
  • 1
    Please fix the formatting of your code, it's impossible to read. – Patrick Collins Aug 26 '14 at 10:34
  • 1
    I fixed it for you. You should be consistent with the way you use your curly braces. And that "write a line, indent the open curly brace, indent the body, unindent the curly brace, unindent the next line" looks really strange. – Patrick Collins Aug 26 '14 at 10:37
  • Okay sure thanks. Do you have any input about the question? – matt Aug 26 '14 at 10:40
  • Take a look at this: http://stackoverflow.com/questions/1195809/looking-for-java-telnet-emulator – Patrick Collins Aug 26 '14 at 10:43
  • Okay that looks like it could be semi-useful, all I want to do though is open a connection with a telnet server and listen for new data, does the client need to be that sophisticated? – matt Aug 26 '14 at 11:32

1 Answers1

3

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.

Community
  • 1
  • 1
Patrick Collins
  • 9,307
  • 4
  • 23
  • 60