Domanda

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.

I have made a loginscript to determine if the user is a valid user or not. So I can write two words like "myname" "space" "mypassword" and I get a green light and returns a valid user. 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!

(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...;)

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....

// --------------// 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! :)

È stato utile?

Soluzione 3

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.

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 :)

Altri suggerimenti

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. If it does not, dataInArray will only contain one element and dataInArray[1] will throw an 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
}

The IndexOutOfBoundsExeption more than likely being caused by:

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. 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:

if ("\n".equals(dataInArray[1])) {
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top