简体   繁体   English

处理Java套接字.readLine()中的一个或多个单词

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

I am building an application where I have a server and a client that talk to each other - 我正在构建一个应用程序,其中我有一个服务器和一个客户端,它们彼此通信- over telnet 通过远程登录 . (via socket). (通过套接字)。 The server program is monitoring a tank of some gass, and sends temperature level and preassure level via socket to the accepted clients. 服务器程序正在监视一些气体罐,并通过套接字将温度水平和压力保证水平发送给接受的客户端。

I have managed to get the client and server to talk to each other when I write stuff --in telnet--, but... I need some help to handle the data that I send . 当我在telnet中编写东西时,我设法使客户端和服务器相互通信,但是... 我需要一些帮助来处理发送的数据

I have made a loginscript to determine if the user is a valid user or not. 我做了一个loginscript以确定用户是否是有效用户。 So I can write two words like "myname" "space" "mypassword" and I get a green light and returns a valid user. 因此,我可以写两个单词,例如“ myname”,“ space”,“ mypassword”,然后我会获得绿灯并返回有效用户。 But when I only write one word, and hit enter, it gives me: Exeption in thread... java.lang.Array.IndexOutOfBoundsExeption EXEPT for when I write exit or logout! 但是,当我只写一个单词并按Enter键时,它会给我: 线程中的 Exeption ... java.lang.Array.IndexOutOfBoundsExeption当我编写退出或注销时的EXEPT!

(All users are hardcoded in the script for ease of use for testing. (The login script works fine by it self, and returns valid user = false when I write something wrong.) Here is my code. Some pseudo code is added since I am not 100% sure of what to do...;) (为了方便测试,所有用户都在脚本中进行了硬编码。(登录脚本本身可以很好地工作,当我写错东西时返回有效的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?
            }

The client application has a button for each command, like: 客户端应用程序为每个命令都有一个按钮,例如:

"Send me every n'th data", or "Send me a batch of data every n'th second. If command equals exit, or logout - > break operation.... “每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;
            }

Maybe I am getting a bit confused now, but I think that I need to put all data into an array, and check 也许我现在有点困惑,但是我认为我需要将所有数据放入一个数组,然后检查

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

and... 和...

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

Thanks for any help, and/or tips! 感谢您的帮助和/或提示! :) :)

The IndexOutOfBoundsExeption more than likely being caused by: IndexOutOfBoundsExeption可能是由以下原因引起的:

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

Make sure that the incoming String telNetCommand contains at least one space so that you have at 2 Strings in the array. 确保传入的String telNetCommand至少包含一个空格,以便数组中有2个Strings You could do this checking the size of the array: 您可以执行以下操作检查数组的大小:

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

Also, on a different note, make sure to use String.equals when checking String content: 另外,请注意,在检查字符串内容时,请确保使用String.equals

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

In this part of your code: 在这段代码中:

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

You assume that the telNetCommand string contains a space. 您假定telNetCommand字符串包含一个空格。 If it does not, dataInArray will only contain one element and dataInArray[1] will throw an IndexOutOfBoundsExeption . 如果不是,则dataInArray仅包含一个元素,而dataInArray[1]将抛出IndexOutOfBoundsExeption

You should check the size of the array: 您应该检查数组的大小:

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

Thanks guys. 多谢你们。 I don't get any errors now... And here is what I ended up doing. 我现在没有任何错误...这就是我最终要做的。 I had to set it == 2 in order not to get any errors. 我必须将其设置为== 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;
                }
            }
        }

Peace :) 和平:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM