2017-10-28 159 views
0

我有一個FXML接口+中,我試圖完成以下Java程序:的InputStream Telnet控制檯FXML文本區

  1. 一個按鈕被點擊的FXML接口上。
  2. 該程序啓動到服務器的遠程登錄。
  3. 本程序登錄到服務器。
  4. 本程序在服務器上執行命令。
  5. 程序斷開與服務器的連接。 整個telnet會話的輸入+輸出應該流式傳輸到FXML接口上的TextArea。

Telnet法的工作,我可以流與輸入和輸出到控制檯:

public class TelnetApplication 
    TelnetClient tl = new TelnetCient(); 
    InputStream is; 
    PrintStream ps; 
    String Prompt = "$ " 

    public TelnetApplicaton(String server, String user, String Password, String command) { 
     try { 
      tl.connect(server, 23); 
      is = tl.getInputStream(); 
      os = new PrintStream(tl.getOutputStream()); 
      readUntil("Login: "); 
      inputText(user); 
      readUntil("Password: "); 
      inputText(password); 
      readUntil(prompt); 
      inputText(command); 
      readUntil(prompt); 
      tl.disconnect(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
    } 
    public String readUntil(String p) { 
     try { 
      char lastChar = p.charAt(p.length() - 1); 
      StringBuffer sb = new StringBuffer(); 
      boolean found = false; 
      char ch = (char is.read(); 
      while (true) { 
       System.out.print(ch); 
       sb.append(ch); 
       if (ch == lastChar) { 
        if (sb.toString().endsWith(p)) { 
         return sb.toString(); 
        } 
       } 
       ch = (char) is.read(); 
      } 
     } catch Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    public void inputText(String s) { 
     try { 
      os.println(s); 
      os.flush(); 
      System.out.println(s); 
     } catch (Exception e) { 
      e.printStackTrace(); 
    } 
} 

的FXM控制器看起來是這樣的:

public class FXMLController { 
    @FXML TextArea actionConsole; 

    @FXML protected void handleButtonClick(ActionEvent event) { 

     TelnetApplication tl = new TelnetApplication("192.168.0.1", "user", "password", "df -h"); 
    } 
} 

所以我的問題是,我怎樣才能確定流輸入和輸出的值,這些值可以顯示在FXML接口的TextArea actionConsole中? 我已經嘗試了很多東西,但沒有看到工作。我必須承認,我缺乏編程經驗可能是我的問題的根源。 在此先感謝。

回答