Search code examples
javasocketstelnet

opening a Java socket vs. Telnet?


If I execute the following code (NOTE: not the real IP address being used)

Socket s = new Socket("120.200.100.111", 80);
s.setSoTimeout(10 * 1000);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
in.readLine();

After 10 seconds, I get 'SocketTimeoutException: Read timed out'

But if I telnet, I get:

telnet> open 120.200.100.111 80
Trying 120.200.100.111...
Connected to foo.bar.org.
Escape character is '^]'.

Please note I am aware that for a full application I should use some sort of existing API for socket communications, but shouldn't the above test be getting some sort of timely response from the server?


Solution

  • shouldn't the above test be getting some sort of timely response from the server?

    No. A Telnet exchange doesn't start with the server sending you anything. You have to send something. The server is waiting for you; you are waiting for it; you're the one with the read timeout, so you get a read timeout.

    The Telnet client doesn't use read timeouts by default.