2009-08-14 40 views
1

我有一個Java程序在Linux和Telnet上運行到使用org.apache.commons.net.telnet.TelnetClient的遠程服務器並執行一些命令。問題在於,當它到達一個輸出顯示,並要求用戶「按任意鍵繼續...」時,它會間歇性地掛起。程序在每運行10次,每運行10次,運行它的7臺服務器中就有1次掛起只有3臺服務器有問題。另外,當我在Windows盒子上運行相同的程序時,它一直都在運行。Java TelnetClient掛起在「按任意鍵繼續」

我想知道是否有其他人遇到過這樣的問題?

在測試服務器上,我可以每次都將它掛起來進行測試。我試圖發送其他命令不會導致掛起,但沒有運氣。我嘗試了所有的回報,換行符,添加一個字符並放入一個換行符。似乎沒有任何東西能夠讓客戶繼續。

忘了提及沖洗緩衝區什麼我想到的第一件事。我把flush命令放在任何我認爲可能會發生的地方。
我還會提到,當我運行它並觀察寫入行的輸出時,它會發現「按任意鍵」並繼續前進,但掛起終端不會繼續。

代碼,我打這個電話:

 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(); 
    } 
} 

堆棧跟蹤:java.net.SocketTimeoutException:閱讀java.net.SocketInputStream.socketRead0(本機方法) 超時 在java.net.SocketInputStream。在java.io.BufferedInputStream.fill(BufferedInputStream.java:218) 處讀取(SocketInputStream.java:129) at java.io.BufferedInputStream.read(BufferedInputStream.java:237) at java.io.FilterInputStream.read( FilterInputStream.java:66) at java.io.PushbackInputStream.read(PushbackInputStream.java:12 2) at org.apache.commons.net.io.FromNetASCIIInputStream .__ read(FromNetASCIIInputStream.java:77) at org.apache.commons.net.io.FromNetASCIIInputStream.read(FromNetASCIIInputStream.java:175) at java。 (BufferedInputStream.java:218) at java.io.BufferedInputStream.read 在org.apache.commons.net.telnet.TelnetInputStream.run(TelnetInputStream.java:564) 在java.lang.Thread.run(Thread.java:619)

WHERE它掛起: 英語1 6000 4462 26%13826 11056 20%

Calls answered since Thu Jun 4, 2009 3:11 am: 41245 

按任意鍵繼續....

+0

你使用緩衝流嗎?也許有些人需要調用.flush來確保你的角色實際上是立即傳輸的? – jsight 2009-08-14 15:10:25

+0

你可以發佈你的源代碼的一個片段,你發佈導致服務器掛起的命令嗎? – ChssPly76 2009-08-14 15:24:08

+0

當您的應用程序掛起時,您是否嘗試獲取堆棧跟蹤(例如,通過發送一個kill -QUIT信號)? – Adamski 2009-08-14 15:26:06

回答

4

可能有幾個原因:

  1. 你不沖水的輸出(遠程命令的輸入),所以「任何密鑰」都不會被髮送。

  2. 該程序試圖向您發送一些數據,並且您從不讀取您的輸入(遠程命令的輸出)。請注意,您必須在第二個線程中執行此操作,因爲I/O通常「同時」發生,如果您沒有及時處理對方,則一方會阻塞。

  3. 也許你遇到問題,因爲應用程序將終端轉換爲「RAW模式」。但沖洗你的輸出應該可以解決這個問題:/

+0

我已經看過這兩個,我正在沖洗我的緩衝區。 (很多次,在許多地方)。我也尋找額外的文字,但沒有看到任何。 – Ben 2009-09-11 17:37:06

相關問題