简体   繁体   English

Java-客户端收到来自in.readLine()的空响应

[英]Java - client receiving a null response from in.readLine()

I'm a long way into writing a card game that allows people to play online against each other. 我在写纸牌游戏方面做得很长,这使得人们可以在线对战。 During testing I've noticed that clients are very occasionally getting a null response to their messages and I don't know why. 在测试过程中,我注意到客户偶尔会收到对邮件的空响应,我不知道为什么。

Client code: 客户代码:

InetAddress address = InetAddress.getByName(SERVER_IP);
Socket socket = new Socket(address, SERVER_PORT_NUMBER);
socket.setSoTimeout(20000);

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.write(messageString + "\n");
out.flush();

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
responseString = in.readLine();

//new handling I've added since getting NullPointers later on
if (responseString == null)
{
    retryOrStackTrace(new Throwable("NULL responseString"));
}

Server code: 服务器代码:

BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
message = in.readLine();
String response = getResponseForMessage(message);

//Sometimes getting a null response client-side
if (response == null)
{
    Debug.stackTraceServerWithReason("About to send NULL response for message " + message);
}

BufferedOutputStream os = new BufferedOutputStream(clientSocket.getOutputStream());
OutputStreamWriter osw = new OutputStreamWriter(os, "US-ASCII");
osw.write(response + "\n");
osw.flush();
osw.close();

The server-side stack trace hasn't come out since I added it so it definitely isn't a problem with the server building a response. 自从我添加以来,服务器端堆栈跟踪就没有出现过,因此服务器构建响应绝对不是问题。

Due to how intermittent this is I want to put it down to connectivity, but I've had it when running the server on the same box and pointing the client at localhost so I'm not sure. 由于这是多么的断断续续,所以我想将其归结为连通性,但是当我在同一盒子上运行服务器并将客户端指向本地主机时,就已经有了它,所以我不确定。 I'd also expect any connection issues to be covered by ConnectException/SocketException/SocketTimeoutException, all of which the client already catches before using retryOrStackTrace() to resend the message a certain number of times before giving up. 我还希望ConnectException / SocketException / SocketTimeoutException可以解决任何连接问题,在使用retryOrStackTrace()在放弃之前重新发送消息一定次数之前,客户端已经捕获了所有连接问题。

I can leave things as they are and let the null responses be handled in the same way but it feels lazy. 我可以保留原样,并以相同的方式处理null响应,但是感觉很懒。 I just don't know where to start in figuring out what the underlying issue is here. 我只是不知道从哪里开始找出这里的根本问题。

Looking back at this I believe the issue was around breakpointing/hotswapping code by building in Eclipse. 回顾这一点,我认为问题在于通过在Eclipse中进行构建来实现断点/热交换代码。 I only ever saw this problem when running through Eclipse - running through a JAR works fine. 我只在通过Eclipse运行时才看到此问题-通过JAR运行可以正常工作。 D'oh! D'哦!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM