好吧,所以我创建了一个客户端和服务器,它使用基于文本的协议通过GUI界面操作Map。下面的处理程序用于创建“工作单”以在与网络通信不同的线程上操作GUI。
class RemoteInputHandler implements Runnable,SharedVariables
{
@Override
public void run()
{
try
{
String input = netComm.reader.readLine();
while (input != null)
{
// Make a separate copy of the input string
String inputCopy = input;
// Post a work order to process the command on the GUI thread
Platform.runLater(() ->
{
handleRemote(inputCopy);
});
// Get the next remote input
input = netComm.reader.readLine();
}
} catch (IOException ex)
{
throw new RuntimeException(ex);
}
}
这会从服务器中提取下一行输入,而不会冻结GUI。然后,我使用handleRemote()方法中的输入和扫描程序来确定输入的内容。从阅读器检索的字符串看起来像“put key value”。然后我使用扫描仪获得第一个单词“put”来获取“命令”并使用switch语句来确定如何在客户端和服务器端更新GUI / Map。
我正在做另一个基于GUI的程序,它使用二进制协议代替,但我无法像上面的readLine()那样弄清楚如何处理信息。有没有办法做到这一点?我在考虑错误的方式吗?我以为我可以将所有字节都放到一个数组中,但是我甚至无法解决这个问题。
我真的可以使用提示!谢谢!