gpt4 book ai didi

Java BufferedReader 接收空字符串

转载 作者:行者123 更新时间:2023-12-01 14:30:03 24 4
gpt4 key购买 nike

我的程序经过精心设计,以便主应用程序可以向连接到它的任何节点发送命令。当节点收到请求时,它会返回响应并继续等待更多请求。

当应用程序运行时,节点成功回复一个请求,而当发送第二个请求时,节点将其视为 null 或根本看不到它。为什么这种情况不断发生?

附注我希望与节点的连接保持打开状态,以便它可以接收更多请求。

请求发送代码:

public java.lang.String getTime(server.Node node){

protocol.Message ms = new protocol.Message("<time>","");

node.sendRequestToClient(ms);
node.dos.flush();

java.lang.System.out.println("Sent time request to " + node.ip);

java.lang.String time = null;
try {

time = node.dis.readLine();


} catch (java.io.IOException ex) {
System.out.println("Could not read response.");
}

protocol.Message response = protocol.Message.parseDataToMessage(time);

java.lang.String systime = response.getActionData();

return systime;


}

响应发送代码:

public class Client {

public Client(NetworkConnection connection){


this.connectionToServer = connection;

try{
connectionToServer.connect();
responseOutStream = connectionToServer.getPrintWriter();
requestInStream = connectionToServer.getBufferedReader();
}catch(IOException ex){
System.out.println("Could not connect to server." + ex.getMessage() + ex.toString());
}



}

public void beginRequestListener(){

String request;

try {
while((request = requestInStream.readLine())!=""){


System.out.println("Recieved request: " + request + request.length());

Message response = Message.parseDataToMessage(request);

sendResponseToServer(response);
}


} catch (java.io.IOException ex) {
System.out.println("Could not read request stream.");
} catch(NullPointerException e){
e.printStackTrace();
e.getClass();
}

}

public void sendResponseToServer(Message ms){

protocol.Message response = MessageParser.compileResponse(ms);
java.lang.System.out.println("Response to send: "+response);
response.send(responseOutStream);



}



public BufferedReader requestInStream;
public PrintWriter responseOutStream;
public NetworkConnection connectionToServer;

}

消息解析器类:

public class MessageParser {



static public Message compileResponse(Message ms){

Message response = null;

switch(ms.getAction()){

case "<time>":

response = new Message("<time>", String.valueOf(System.currentTimeMillis()));
break;

case "<date>":

SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
Date date = new Date();
sd.setTimeZone(TimeZone.getTimeZone("IST"));



response = new Message("<date>", date.toString());
break;



default:

break;

}
return response;
}

}

堆栈跟踪和输出:

Recieved request: <action><time><action><actionData><actionData>
Response to send: <action><time><action><actionData>1370380854566<actionData>

Recieved request:
java.lang.NullPointerException
at protocol.MessageParser.compileResponse(MessageParser.java:23)
at client.Client.sendResponseToServer(Client.java:67)
at client.Client.beginRequestListener(Client.java:52)
at client.ClientInterface.main(ClientInterface.java:107)

消息类别:

公开课消息{

public Message(String data){



}

public Message(String action, String actionData){

this.action = action;
this.actionData = actionData;

}

public void send(PrintWriter connection){

try{
connection.println(this.toString());
connection.flush();
//System.out.println(this.toString());
}catch(Exception ex){
System.out.println("Could not send Message.");
}


}

@java.lang.Override
public String toString(){

return
action_marker + action + action_marker +
actionData_marker + actionData + actionData_marker +
eof_marker;

}

public static Message parseDataToMessage(String data){


Message ms = null;
if(data.isEmpty() == false){
int begin_action_marker = data.indexOf("<action>")+8;
int end_action_marker = data.lastIndexOf("<action>");

String action = data.substring(begin_action_marker, end_action_marker);

int begin_actionData_marker = data.indexOf("<actionData>")+12;
int end_actionData_marker = data.lastIndexOf("<actionData>");

String actionData = data.substring(begin_actionData_marker, end_actionData_marker);

ms = new Message(action, actionData);



}

return ms;

}

public void setAction(String action){
this.action = action;

}

public String getActionData(){
return actionData;

}

public String getAction(){
return action;

}

public void setActionData(String action){
this.actionData = action;

}

public String eof_marker = "\r\n";

public String action;
public String action_marker = "<action>";

public String actionData;
public String actionData_marker = "<actionData>";

}

最佳答案

我的猜测:

  • 您在 (request = requestInStream.readLine()) 中收到一个空请求
  • 这会转到 Message.parseDataToMessage(request);,它会为空请求返回 null
  • compileResponse 中生成 NullPointerException

(可能的)解决方案:更改此设置

while((request = requestInStream.readLine())!=""){

进入此:

while(!(request = requestInStream.readLine()).isEmpty())

为什么你的代码不起作用:How do I compare strings in Java?

关于Java BufferedReader 接收空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16927873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com