如何在java中读取inputstreams时消除命令提示符中的重复消息
如果输入xml消息并以>>>结尾,则从commnad prompt Simulator读取xml消息,表示消息一结束,即每次按enter键时,它都会调用服务器端线程并处理消息。如果我再输入一条xml消息,那么我会得到第一条消息和最后一条消息,但我不想要第一条消息,所以请给我消除它的解决方案 下面的代码位于线程内部,它将始终侦听消息并将其发送到服务器,同时发送“我只想要最新消息”如何在java中读取inputstreams时消除命令提示符中的重复消息,java,multithreading,network-programming,simulator,Java,Multithreading,Network Programming,Simulator,如果输入xml消息并以>>>结尾,则从commnad prompt Simulator读取xml消息,表示消息一结束,即每次按enter键时,它都会调用服务器端线程并处理消息。如果我再输入一条xml消息,那么我会得到第一条消息和最后一条消息,但我不想要第一条消息,所以请给我消除它的解决方案 下面的代码位于线程内部,它将始终侦听消息并将其发送到服务器,同时发送“我只想要最新消息” while(true){ if(clientsoc != null){
while(true){
if(clientsoc != null){
tempchar=br.readLine();
inputstring=inputstring+tempchar;
if(inputstring.endsWith(">>>")){
inputstring=inputstring.replaceAll(">>>","");
serverstream.write(inputstring.getBytes());
}
}
catch (Exception e) {
try {
serverstream.close();
serverstream = null;
clientsoc.close();
clientsoc = null;
} catch (Exception e1) {
clientsoc = null;
serverstream = null;
}
} }
如果我没弄错的话,当用户输入如下字符串时,您只需要这样:
"message1>>>message2>>>"
这是发送到服务器的内容:
"message2"
在这种情况下,一个非常简单的条件检查可以是:
if (inputstring.endsWith(">>>")) {
inputstring = inputstring.substring(0, inputstring.length()-3);
int previousSeparatorIdx = inputstring.lastIndexOf(">>>");
if (previousSeparatorIdx >= 0) {
inputstring = inputstring.substring(previousSeparatorIdx+3);
}
serverstream.write(inputstring.getBytes());
}
通过添加inputstring=,清除以前的消息;在serverstream之后。写入。。。;?我已经做了,但它不起作用。它到底是怎么不正常的?