如何解决readLine() 用 Socket 阻塞
我知道已经有相关主题了,但我还没有找到问题的答案。我正在制作客户端/服务器应用程序。我希望我的客户只在以 \n 结尾时收到消息。但我也不希望它在服务器没有发送消息时阻塞。
目前我的代码是这个。
public void continuousStreaming(){
String messageSend = "ping 12345";
String messageReceive = null;
try{
while(connected){
if(parserCommand.commandToSend()){
messageSend = parserCommand.getCommand();
System.out.println("We got a commande : " + messageSend);
}
out.println(messageSend); //Send to the server
out.flush();
if(in.ready()){ //We get a message only if the buffer is not empty.
messageReceive = in.readLine(); //Wait the response to the client.
messageTreatment(parseMessage(messageReceive));
}
messageSend = "ping 12345";
}
}catch(IOException error){
error.printStackTrace();
}
}
你知道我该如何解决这个问题吗?
感谢您的回答。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。