只能从控制台读取一行输入,然后什么也不会发生:Java

发布于 2024-12-10 04:52:50 字数 1291 浏览 0 评论 0原文

您可以看到我发布的代码 在这里。我原来的问题或多或少得到了解决,但现在我遇到了问题标题中描述的问题。问题是:在任何给定客户端的客户端输入命令后,我无法输入其他命令。第一个命令可以正常工作(减去 /exit;还没有完全弄清楚它应该如何工作),只是之后无法执行任何操作。例如,我可以执行/注册(登录),但之后就什么都没有了。我可以进入客户端的另一个实例,并使用 /list 列出所有当前用户(有效),但同样,此后我无法输入其他命令 - 控制台不会在第一个命令之后接受它们。知道可能发生什么原因导致这种情况吗?

这是我所指的地方(完整的代码):

while((keyboardInput = keyboardInputScanner.nextLine()) != null){
                System.out.println("Input '" + keyboardInput + "' read on client side.");
                if(keyboardInput.equals("/exit")){
                    socketOut.println("/exit");
                    socketOut.close();
                    socketIn.close();
                    serverSocket.close();
                }else{
                    socketOut.println(keyboardInput);
                }
                while((serverInput = socketIn.readLine()) != null){
                    System.out.println(serverInput);
                }
            }

我相对确定这与不打破内部 while 循环有关,但我不知道有什么办法可以解决它。如果我将 while 循环放在键盘输入循环之外,我将永远不会从服务器返回任何内容。

You can see my posted code here. My original problem was more or less solved but now I'm running into the problem described in the question title. Here's the problem: After I enter a command on the client side of any given client, I can't enter additional commands. The first command will work fine (minus /exit; haven't quite figured out how that should work yet), just can't do anything after that. For example, I can do /register (sign in) but after that nothing else. I can go into another instance of Client and list all the current users with /list as well (works), but again, after that I cannot enter additional commands - the console will not take them after the first one. Any idea what may be happening to cause this?

Here is about where I'm referring to (Code in its entirety):

while((keyboardInput = keyboardInputScanner.nextLine()) != null){
                System.out.println("Input '" + keyboardInput + "' read on client side.");
                if(keyboardInput.equals("/exit")){
                    socketOut.println("/exit");
                    socketOut.close();
                    socketIn.close();
                    serverSocket.close();
                }else{
                    socketOut.println(keyboardInput);
                }
                while((serverInput = socketIn.readLine()) != null){
                    System.out.println(serverInput);
                }
            }

I'm relatively sure it's something to do with not breaking out of the inner while loop, but I don't know any way around it. If I put that while loop outside of the keyboard input loop, I'll never get anything back from the server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿萝 2024-12-17 04:52:50

我认为问题出在这里:

while((serverInput = socketIn.readLine()) != null){
    System.out.println(serverInput);
}

这将无限循环(好吧,直到套接字在另一端关闭)。因此,您永远不会到达外循环的第二次迭代。

您可能希望将响应限制为一行,或者使用其他一些服务器机制来让客户端知道何时停止从 socketIn 读取(例如,在每个服务器的末尾发送一个空行)响应并让客户端在看到该响应时跳出内循环)。

I think the problem is here:

while((serverInput = socketIn.readLine()) != null){
    System.out.println(serverInput);
}

This will loop indefinitely (well, until the socket is closed at the other end). You therefore never get to the second iteration of the outer loop.

You might want to either limit the response to one line, or have some other mechanism for the server to let the client know when to stop reading from socketIn (e.g. send an empty line at the end of every server response and have the client break out of the inner loop when it sees that).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文