2012-10-21 33 views
0

我正在構建一個應用程序,其中有一個服務器和一個客戶端互相通信 - 通過telnet。 (通過插座)。服務器程序正在監視一些氣體罐,並通過套接字將溫度級別和安全級別發送給接受的客戶端。在Java Socket中處理一個或多個單詞.readLine()

我設法讓客戶端和服務器相互交談時,我寫的東西--in telnet--,但是...... 我需要一些幫助來處理,我送數據。

我已經做了一個loginscript來確定用戶是否是有效的用戶。 所以我可以寫兩個詞,如「我的名字」「空格」「我的密碼」,我得到一個綠燈,並返回一個有效的用戶。 但是,當我只寫一個單詞,並按回車,它給了我: 在線程中的執行... java.lang.Array.IndexOutOfBoundsExeption EXEPT當我寫退出或註銷! (登錄腳本可以自行工作,並且在我寫錯的時候返回有效的用戶= false) 這裏是我的代碼,一些僞代碼代碼被添加,因爲我不知道的100%,怎麼辦...;)

String telNetCommand = dataIn.readLine(); 
      System.out.println(telNetCommand); 

      String dataInArray[] = telNetCommand.split(" "); 

      user.isValid(dataInArray[0], dataInArray[1]); 

      if (dataInArray[1] == "\n") { 
      //Ignore login request and continue telnet-logging? 
      } 

的客戶端應用程序爲每個命令按鈕,如:

「送我的每第n個數據「,或」每隔n秒向我發送一批數據,如果命令等於退出,或者註銷 - >中斷操作...「,」

// --------------// USER INPUT FROM CLIENT APP //--------------------------// 

      // --------------// CONTINUE ? //----------------------------// 
      if (command.equals("CONTINUE")) { 
       continueSession(); 
       else { //..Kill session 
       } 
      } 

      // --------------// SKIP <N> //----------------------------// 
      if (command.equals("SKIP_N")) { 
       skipEveryNthData(); 
      } 

      // --------------// BATCH <N> //---------------------------// 
      if (command.equals("BATCH_N")) { 
       batchEveryNthData(); 
      } 

      // --------------// LOGG OUT #1 //-------------------------// 
      if (command.equals("logout") || command.equals("exit")) { 
       break; 
      } 

也許我現在變得有點混亂,但我認爲我需要把所有的數據到一個數組,並檢查

if 
dataInArray[0] == "CONTINUE" 
dataInArray[0] == "SKIP_N", or 
dataInArray[0] == "BATCH_N" 
(then send some data back)... 

和...

if dataInArray[1] == "enter" ("\n") execute the single word commands ...?? 
if dataInArray[0] == "LOG_IN" or "PASSWORD" check if valid user is true.. 

謝謝任何幫助,和/或提示! :)比

+0

*「它給了我:在線程Exeption ... java.lang.Array.IndexOutOfBoundsExeption」 * =>異常消息也應該有對應的行,其中行號發生問題。 – assylias

+0

是啊......我知道。抱歉。它在這裏給我一個錯誤:user.isValid(dataInArray [0],dataInArray [1]); – CustomCase

+0

什麼是由'System.out.println(telNetCommand)打印的;'什麼時候失敗? – Reimeus

回答

1

謝謝你們。我現在沒有得到任何錯誤......而這就是我最終做的。 我不得不將它設爲== 2,以免發生任何錯誤。

while (true) { 
      String telnetCommand = dataIn.readLine(); 

      System.out.println(telnetCommand); 

      String dataInArray[] = telnetCommand.split(" "); 

      if (dataInArray.length == 2) { 
       user.isValid(dataInArray[0], dataInArray[1]); 
      } 

      if (dataInArray.length < 2) { 
       if (telnetCommand.equals("CONTINUE")) { 
        continueThisSession(); 
        System.out.println("Running method continueThisSession"); 
       } 

       if (telnetCommand.equals("SKIP_N")) { 
        skipEveryNthData(); 
        System.out.println("Running method skipEveryNthData"); 
       } 

       if (telnetCommand.equals("BATCH_N")) { 
        batchEveryNthData(); 
        System.out.println("Running method batchEveryNthData"); 
       } 

       if (telnetCommand.equals("logout") || telnetCommand.equals("exit")) { 
        break; 
       } 
      } 
     } 

和平:)

1

IndexOutOfBoundsExeption更可能是由引起:

user.isValid(dataInArray[0], dataInArray[1]); 

確保傳入StringtelNetCommand至少包含一個空間,讓你在數組中2 Strings。你可以這樣做檢查數組的大小:

if (dataInArray.length < 2) { 
    throw new IllegalArgumentException(telNetCommand + " only contains " + dataInArray.length + " elements"); 
} 

此外,在不同的音符,確保檢查字符串的內容時使用String.equals

if ("\n".equals(dataInArray[1])) { 
1

在您的這部分代碼:

String dataInArray[] = telNetCommand.split(" "); 
user.isValid(dataInArray[0], dataInArray[1]); 

您認爲telNetCommand字符串包含空格。如果沒有,dataInArray將只包含一個元素,dataInArray[1]將拋出IndexOutOfBoundsExeption

您應檢查數組的大小:

if (dataInArray.length < 2) { 
    //no space in the command - do what you need to do 
    //for example an error message 
} 
+0

所以你的意思是我應該交換如果(dataInArray [1] ==「\ n」)與if(dataInArray.length <2)?我試圖將所有內容都包含在一個大的「if」語句中。如果「一個字」這樣做,否則如果「兩個字」這樣做。但仍然有錯誤(同樣的錯誤)...:| – CustomCase

相關問題