我正在構建一個應用程序,其中有一個服務器和一個客戶端互相通信 - 通過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..
謝謝任何幫助,和/或提示! :)比
*「它給了我:在線程Exeption ... java.lang.Array.IndexOutOfBoundsExeption」 * =>異常消息也應該有對應的行,其中行號發生問題。 – assylias
是啊......我知道。抱歉。它在這裏給我一個錯誤:user.isValid(dataInArray [0],dataInArray [1]); – CustomCase
什麼是由'System.out.println(telNetCommand)打印的;'什麼時候失敗? – Reimeus