Writng a Java Telnet Client

   I am now writing a Java Client Telnet program to telnet to other server. I have new a Socket on the server on Port 23 which is the default telnet port. However, when I run the program:

c:>java JavaTelnet xxx.yyy.com

server is : xxx.yyy.com
server socket connected! Socket[addr=xxx.yyy.com/,port=23, localport=1212]

then.. the program keep waiting and has no any reply from the server. Can anyone tell me what have I missed? Thanks!

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

public class JavaTelnet {
    public static void main(String[] args) throws IOException {

          //The args should be the server host name
        Socket telnetSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;
        String host = null;
        if (args!=null && args.length>0){
          if (args[0]!=null && !args[0].equals("")){
               host = args[0];
                  try {
                      telnetSocket = new Socket(host, 23);
                      out = new PrintWriter(telnetSocket.getOutputStream(), true);
                      in = new BufferedReader(new InputStreamReader(
                  } catch (UnknownHostException e) {
                      System.err.println("Don't know about host: " + host + ".");
                  } catch (IOException e) {
                      System.err.println("Couldn't get I/O for "
                                         + "the connection to: "+ host +".");
                  System.out.println("server is : " + host);
                  System.out.println("server socket connected! " + telnetSocket.toString());
                  System.out.println("from server: " + in.readLine());
               BufferedReader stdIn = new BufferedReader(
                                             new InputStreamReader(System.in));
               String userInput;
               while ((userInput = stdIn.readLine()) != null) {
                   System.out.println("telnet: " + in.readLine());
Who is Participating?
objectsConnect With a Mentor Commented:
It's not that simple, read the telnet rfc:


Here's a telnet client with source:


Thanks for the points and best of luck :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.