簡體   English   中英

處理Java套接字.readLine()中的一個或多個單詞

[英]Handle one or multiple words in Java Socket .readLine()

我正在構建一個應用程序,其中我有一個服務器和一個客戶端,它們彼此通信- 通過遠程登錄 (通過套接字)。 服務器程序正在監視一些氣體罐,並通過套接字將溫度水平和壓力保證水平發送給接受的客戶端。

當我在telnet中編寫東西時,我設法使客戶端和服務器相互通信,但是... 我需要一些幫助來處理發送的數據

我做了一個loginscript以確定用戶是否是有效用戶。 因此,我可以寫兩個單詞,例如“ myname”,“ space”,“ mypassword”,然后我會獲得綠燈並返回有效用戶。 但是,當我只寫一個單詞並按Enter鍵時,它會給我: 線程中的 Exeption ... java.lang.Array.IndexOutOfBoundsExeption當我編寫退出或注銷時的EXEPT!

(為了方便測試,所有用戶都在腳本中進行了硬編碼。(登錄腳本本身可以很好地工作,當我寫錯東西時返回有效的user = 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..

感謝您的幫助和/或提示! :)

IndexOutOfBoundsExeption可能是由以下原因引起的:

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

確保傳入的String telNetCommand至少包含一個空格,以便數組中有2個Strings 您可以執行以下操作檢查數組的大小:

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

另外,請注意,在檢查字符串內容時,請確保使用String.equals

if ("\n".equals(dataInArray[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
}

多謝你們。 我現在沒有任何錯誤...這就是我最終要做的。 我必須將其設置為== 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;
                }
            }
        }

和平:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM