title: Telnet Java Socket Test
body: <p>I am in need of some direction. Not sure if I am on the right path but I think so. I am trying to create a telnet java program that will connect to a client machine, execute a single command then disconnect. I am able to get the program to work and readout the InputStream to a Text field ( for testing purposes) when I connect to a linux machine or my router. But when I connect to a Windows machine or other client computer, it doens't work. It reads out some random characters, then locks up.</p>
  
  <p>Below is my code. I have seen examples of other code out there as well as API's from Apache for example. I would really like to see if I can get this to work with just Java Sockets.</p>
  
  <pre><code>    public class TestSockets extends JFrame implements ActionListener {
  
  /**
   * @param args
   */
  private String USER = "User";
  private String PASS = "Password01";
  private final static String CMD = "exit\r\n";
  private static Socket telnet = null;
  private PrintWriter writer = null;
  private static InputStream reader = null;
  private String host = "192.168.1.1";
  private int port = 23;
  TextArea javatext;
  
  public static void main(String[] args) {
      // TODO Auto-generated method stub
      new TestSockets().setVisible(true);
  }
  
  private TestSockets() {
      super("Testing Buttons");
  
      //Set JFrame size
      setSize(500, 600);
  
      //Gives JFrame a location
      setLocation(100, 100);
  
      //set layout
      setLayout(new FlowLayout());
  
      javatext = new TextArea(25, 65);
  
      add(javatext);
  
      //Ask for window decorations provided by the look and feel.
      JFrame.setDefaultLookAndFeelDecorated(true);
      JButton button3 = new JButton("Run Program");
      button3.addActionListener(this);
      add(button3);
  }
  
  @Override
  public void actionPerformed(ActionEvent arg0) {
      // TODO Auto-generated method stub
      try {
          telnet = new Socket(host, port);
          telnet.setKeepAlive(true);
          //reader = telnet.getInputStream();
          writer = new PrintWriter(telnet.getOutputStream());
          reader = telnet.getInputStream();
          //out = telnet.getOutputStream();
          //Process p = Runtime.getRuntime().exec("telnet " + server.toString(), null, null);
          //DataOutputStream os = new DataOutputStream(p.getOutputStream());
          //DataInputStream in = new DataInputStream(p.getInputStream());
  
          StringBuilder sb = new StringBuilder();
          byte[] buffer = new byte[4096]; // Read 4K characters at a time
          int len; // How many chars read each time
            while ((len = reader.read(buffer)) != -1) {
               String readchar = new String(buffer, 0, len);
               sb.append(readchar + "\n");
               System.out.println(readchar);
               javatext.append(readchar);
               if (readchar.endsWith("login: ")) {
                   writer.print(USER + "\r\n");
                   writer.flush();
               }
               if (readchar.endsWith("Password: ")) {
                   writer.print(PASS + "\r\n");
                   writer.flush();
               }
               if (readchar.endsWith("password: ")) {
                   writer.print(PASS + "\r\n");
                   writer.flush();
               }
               if (readchar.endsWith("# ")) {
                   writer.print(CMD);
                   writer.flush();
               }
               if (readchar.endsWith("# ")) {
                   writer.print(CMD);
                   writer.flush();
               }
  
           }          
  } catch (SocketException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();    
  } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  }
  }
  
      }
  </code></pre>