我目前正在为一个学校项目编写一个小的 CLI,该项目打算引入套接字编程和多线程编程。我们基本上是在实现一种文件传输协议。我目前在格式化 CLI 时遇到了一些问题。由于程序的多线程性质,有时请求输入,然后显示输出。这使得用户和 CLI 之间的整体交互变得尴尬,因为现在用户必须在输出发生后输入输入。
我想知道是否有办法使用标准 Java I/O,即使有新消息传入,也能将输入始终保持在 CLI 的底部。
例如,输出当前如下所示:
Connected to 127.0.0.1:5000 and 127.0.0.1:5001
mytftp> pwd
/Users/Shawn/Desktop/Computer Science/CSCI4780 Distributed Systems/Project2/server
mytftp> pwd &
mytftp>
/Users/Shawn/Desktop/Computer Science/CSCI4780 Distributed Systems/Project2/server
ls
.DS_Store
sdf
clientFile.txt
mytftp>
但我期待这样的事情:
Connected to 127.0.0.1:5000 and 127.0.0.1:5001
mytftp> pwd
/Users/Shawn/Desktop/Computer Science/CSCI4780 Distributed Systems/Project2/server
mytftp> pwd &
/Users/Shawn/Desktop/Computer Science/CSCI4780 Distributed Systems/Project2/server
mytftp> ls
.DS_Store
sdf
clientFile.txt
mytftp>
注意:此处的"&"表示"在单独的线程上运行此命令"。这就是为什么在下一次执行期间,首先显示"mytftp>"提示,然后显示线程的结果。我只是希望当前的提示始终保持在底部。旁注,不会添加 &on pwd 命令以提高性能。它通常仅用于文件上传和下载命令,但此处用作示例。
我不确定这是否可能,但替代方案将不胜感激。
重申一下,该流程希望:
Connection Established:
连接到 127.0.0.1:5000 和 127.0.0.1:5001Command is entered into new thread
:mytftp> pwd &Next prompt is shown ready for a new command
: mytftp>
一旦pwd &
的结果出来,我希望提示下来:
Connection Established:
连接到 127.0.0.1:5000 和 127.0.0.1:5001Command is entered into new thread
: mytftp> pwd &Space was made for the prompt and results shown
:/Users/Shawn/Desktop/Computer Science/CSCI4780 Distributed Systems/Project2/serverNext prompt is shown ready for a new command
: mytftp>
我最终想出的解决方案是使用r
字符并重新排列提示流。而不是:
while true:
show prompt
get input
do some socket programming here
我选择使用以下架构
show prompt
while true:
get input
do some socket programming here and each socket response (system.out) should start with r, followed by the message, followed by "nmytftp>".
这意味着来自服务器的每条消息都将删除当前提示,但使用返回回车,然后将提示添加回去。