【问题标题】:Handle one or multiple words in Java Socket .readLine()在 Java Socket .readLine() 中处理一个或多个单词
【发布时间】:2012-10-21 16:30:06
【问题描述】:

我正在构建一个应用程序,其中我有一个服务器和一个客户端,它们可以相互通信 -通过 telnet。 (通过插座)。服务器程序正在监视一罐气体,并通过套接字将温度水平和压力水平发送到接受的客户端。

当我在 telnet 中写东西时,我已经设法让客户端和服务器相互通信,但是...... 我需要一些帮助来处理我发送的数据

我已经制作了一个登录脚本来确定用户是否是有效用户。 所以我可以写两个词,如“myname”“space”“mypassword”,我得到一个绿灯并返回一个有效的用户。 但是当我只写一个词并按回车时,它给了我: 线程中的异常... java.lang.Array.IndexOutOfBoundsExeption 当我写退出或注销时出现异常!

(所有用户都在脚本中进行了硬编码,以便于测试。(登录脚本本身可以正常工作,当我写错时返回有效用户=假。) 这是我的代码。添加了一些伪代码,因为我不是 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 " => 异常消息还应该有一个对应于行的行号问题出现了。
  • 是的……我知道。对不起。它在这里给我一个错误:user.isValid(dataInArray[0], dataInArray[1]);
  • System.out.println(telNetCommand);失败时打印什么?
  • 它只返回我输入的内容。当我写“Demouser”“demopassword”时它返回“Demouser”“demopassword”......如果我写“SKIP_N”(或任何其他单字..)它失败了。如果我写“注销”或“退出”它会正常终止。
  • 这与 Telnet 无关,除非您使用端口 23 或某处有 RFC 754 的一些实现。

标签: java arrays sockets eol


【解决方案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
}

【讨论】:

  • 所以你的意思是我应该交换 if (dataInArray[1] == "\n") 与 if (dataInArray.length
【解决方案2】:

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])) {

【讨论】:

    【解决方案3】:

    谢谢各位。我现在没有收到任何错误......这就是我最终做的事情。 我必须将它设置为 == 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;
                    }
                }
            }
    

    和平:)

    【讨论】:

      猜你喜欢
      • 2021-02-24
      • 2012-04-20
      • 2012-12-16
      • 1970-01-01
      • 2014-05-27
      • 1970-01-01
      • 2021-07-02
      • 1970-01-01
      • 2014-05-23
      相关资源
      最近更新 更多