views:

766

answers:

4

I have this small test socket connection class:-

import java.net.Socket;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.IOException;

public class TestTelnet {

public static void main(String args[]) throws Exception {
    Telnet telnet = new Telnet(); 
    Socket socket = null ;
    socket = new Socket("localhost", 23);
    socket.setKeepAlive(true);
    BufferedReader r = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        BufferedWriter w = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    System.out.println(r.readLine());
    socket.close();
}
}

It works perfectly well when I use another port (for example 25 for SMTP) and the println of r.readLine works brilliantly. I can also connect to port 23 via the command prompt (telnet localhost 23) and I get the following returned:-

Ubuntu 8.10 my-laptop login:

But when I try and connect to port 23 using my java class, it just hangs on the readLine println. Does anyone know why this is?

+2  A: 

Telnet is a protocol, and is probably expecting you to do option negotiation. Once you send it something useful, it will probably send you something useful.

see: http://www.faqs.org/rfcs/rfc854.html

Christopher
Will I not need to login first? When I connect via cmd I have to enter a username and password?
no, of course not. The server needs to know what options you understand.
Christopher
+3  A: 

I guess it's because your expecting a line (with CR or CRLF termination) but your telnet service does not send a complete line. Try using r.read() instead of r.readLine()

Andreas_D
Thanks for the sugestion. I tried that (using a while (true) { println(r.read()) } I got this:-65533655332465533655333265533 when I tried casting these to chars it just displayed little ?'s!
Here is an open telnet service: rainmaker.wunderground.com, Port 3000. Just providing some weatherdata... Try this one with your test class just to make sure, whether the problem is in the test class or your Telnet class
Andreas_D
... tried it myself, your code works fine with this weather service, the readLine() version aswell as the read() version. So I guess, the problem is on the sender side (Telnet implementation)
Andreas_D
Those bytes are telnet commands for option negotiation. It is expecting a response before continuing.
mark4o
A: 

Telnet is both a protocol and an application

When you use telnet the application to a machine, the application sends the protocol information so the remote machine may respond.

Since you say it stay there, it means it is working, it's just you are not following the protocol.

The protocol is described here:

http://tools.ietf.org/html/rfc854

What you're trying to do here is write a telnet application.

OscarRyz
A: 

A good example of a Telnet client can be found at http://www.java2s.com/Code/Java/Network-Protocol/ExampleofuseofTelnetClient.htm

rtenhove