title: Java TelnetClient hangs at “press any key to continue”
body: <p>I have a Java program that runs on Linux and telnets into a remote server using <code>org.apache.commons.net.telnet.TelnetClient</code> and performs a few commands.  The problem is that it hangs intermittently when it gets to an output display that asks the users to “press any key to continue…”  The program hangs on this about 1 out of every 10 tims it runs and out of the 7 servers I run it on only 3 of the servers have problems.  Also, when I run the same program on a windows box it works all the time.</p>
  
  <p>I was wondering if anyone else has encountered a problem like this?  </p>
  
  <p>On a test server I can get it to hang every time to test with.  I have tried to send in other commands that won’t cause it to hang but no luck.  I have tried all the carrage return, line feed, adding a character and putting in a line feed.  Nothing seems to make it client continue.</p>
  
  <p>Forgot to mention that flushing the buffer what the first thing I thought of.  I put the flush command anywhere I thought it might hlep.<br>
  I will also mention that when I run it and watch the output from the write line it does find the "press any key" and keeps going but hangs the terminal does not continue.</p>
  
  <p>CODE WHERE I MAKE THE CALL:</p>
  
  <pre><code>        readUntil("X) Exit (no report)");
          write("C", false);
          out.flush();
  
          readUntil("continue....");
  
          // write this for all servers.
          write("", true);
          out.flush();
  
          readUntil("X) Exit");
          write("X", false);
  
  
  /*
   * This method is used to read the command line until the pattern that was 
   * passed in is found.
   */
  public String readUntil(String pattern) throws Exception {
      try {
          String tempString;
          char lastChar = pattern.charAt(pattern.length() - 1);
          StringBuffer sb = new StringBuffer();
          //boolean found = false;
          char ch = (char) in.read();
          while (true) 
          {
              // NOTE: Turn line below on to watch the program perform the telnet
              System.out.print(ch);
  
              sb.append(ch);
              tempString = sb.toString();
              if (ch == lastChar) {
                  if (tempString.endsWith(pattern)) 
                  {
                      // log to file
                      logFileWriter.write(tempString);
                      logFileWriter.flush();
                      return tempString;
                  }
              }
              ch = (char) in.read();
          }
      }
      catch (Exception e) {
          e.printStackTrace();
          throw e;
      }
  }
  
  /*
   * writes the String passed in to the command line.
   * boolean userWriteln: true - use the return key after the command, false - just type the 
   * command with NO enter key
   */
  public void write(String value, boolean useWriteln) 
  {
  
      System.out.println("WRITTING '" + value + "'");
  
      try {
          if (useWriteln)
          {
              out.println(value);
          }
          else
          {
              out.print(value);
          }
          out.flush();
          System.out.println(value);
      }
      catch (Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>StackTrace: java.net.SocketTimeoutException: Read timed out
      at java.net.SocketInputStream.socketRead0(Native Method)
      at java.net.SocketInputStream.read(SocketInputStream.java:129)
      at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
      at java.io.BufferedInputStream.read(BufferedInputStream.java:237)
      at java.io.FilterInputStream.read(FilterInputStream.java:66)
      at java.io.PushbackInputStream.read(PushbackInputStream.java:122)
      at org.apache.commons.net.io.FromNetASCIIInputStream.__read(FromNetASCIIInputStream.java:77)
      at org.apache.commons.net.io.FromNetASCIIInputStream.read(FromNetASCIIInputStream.java:175)
      at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
      at java.io.BufferedInputStream.read(BufferedInputStream.java:237)
      at org.apache.commons.net.telnet.TelnetInputStream.__read(TelnetInputStream.java:122)
      at org.apache.commons.net.telnet.TelnetInputStream.run(TelnetInputStream.java:564)
      at java.lang.Thread.run(Thread.java:619)</p>
  
  <p>WHERE IT HANGS:
             english   1      6000    4462    26 %       13826   11056    20 %</p>
  
  <pre><code>Calls answered since Thu Jun  4, 2009  3:11 am: 41245
  </code></pre>
  
  <p>Press any key to continue....</p>