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