BufferedReader readLine 每隔两行跳过一次
我正在使用套接字在服务器和客户端之间进行通信。 但出于某种原因,客户端会跳过服务器发送的每一行。
客户端代码:
...
out.println(console.readLine()); //Client initiates (sent to server)
while ((userOut = in.readLine()) != null) //Waits for response
{
System.out.println("Server says: " + userOut); //Prints response
userIn = console.readLine(); //Gets user input
out.println(userIn); //Sends user input to server
}
...
服务器代码:
...
while ((clientIn = in.readLine()) != null) //Waits for clients message
{
System.out.println("Client says: " + clientIn); //Print clients message
//Send appropriate response
if (clientIn.equals(CLIENT_INSTRUCTION_LOGCALC))
{
out.println(SERVER_RESPONSE_LOGCALC_OK); //Send response to client
System.out.println("Message sent: " + SERVER_RESPONSE_LOGCALC_OK); //Print response sent
}
else if (clientIn.equals(CLIENT_INSTRUCTION_SB))
{
out.println(SERVER_RESPONSE_SB_CHANGE);
}
else if (clientIn.equals(CLIENT_INSTRUCTION_BYE))
{
out.println(SERVER_RESPONSE_BYE_OK);
}
else if (clientIn.equals(CLIENT_INSTRUCTION_END))
{
out.println(SERVER_RESPONSE_END_OK);
}
else
{
out.println(SERVER_RESPONSE_INPUT_ERR);
}
...
使用此显示的示例(首先是客户端):
LOGCALC
Server says: LOGCALC: OK
LOGCALC
Server says:
服务器:
Client says: LOGCALC
Message sent: LOGCALC: OK
Client says: LOGCALC
Message sent: LOGCALC: OK
希望您可以看到在发送到服务器的第二条 LOGCALC 消息中,服务器做出了响应,但客户端没有收到服务器响应。
有什么想法吗?
I'm using sockets to communicate between a server and client.
For some reason though, the client skips every second line that the server has sent.
Client's code:
...
out.println(console.readLine()); //Client initiates (sent to server)
while ((userOut = in.readLine()) != null) //Waits for response
{
System.out.println("Server says: " + userOut); //Prints response
userIn = console.readLine(); //Gets user input
out.println(userIn); //Sends user input to server
}
...
Servers Code:
...
while ((clientIn = in.readLine()) != null) //Waits for clients message
{
System.out.println("Client says: " + clientIn); //Print clients message
//Send appropriate response
if (clientIn.equals(CLIENT_INSTRUCTION_LOGCALC))
{
out.println(SERVER_RESPONSE_LOGCALC_OK); //Send response to client
System.out.println("Message sent: " + SERVER_RESPONSE_LOGCALC_OK); //Print response sent
}
else if (clientIn.equals(CLIENT_INSTRUCTION_SB))
{
out.println(SERVER_RESPONSE_SB_CHANGE);
}
else if (clientIn.equals(CLIENT_INSTRUCTION_BYE))
{
out.println(SERVER_RESPONSE_BYE_OK);
}
else if (clientIn.equals(CLIENT_INSTRUCTION_END))
{
out.println(SERVER_RESPONSE_END_OK);
}
else
{
out.println(SERVER_RESPONSE_INPUT_ERR);
}
...
An example of using this displays (client first):
LOGCALC
Server says: LOGCALC: OK
LOGCALC
Server says:
Server:
Client says: LOGCALC
Message sent: LOGCALC: OK
Client says: LOGCALC
Message sent: LOGCALC: OK
Hopefully you can see that in the second LOGCALC message sent to the server, the server responded, but the client did not receive the servers response.
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
客户端肯定会收到“某种”类型的消息,因此会打印“服务器说”部分。似乎服务器在向客户端发送“第一个响应”时以某种方式写入了额外的新行,这导致在第二次迭代时读取空字符串。在第二种情况下,值得检查/调试 userOut 的值。
另外,我不确定这是否是故意的,但在服务器输出的情况下我看到一个空行。假设它是发布的代码片段执行所有输出,那么额外的换行符来自哪里?
The client definitely receives "some" sort of message hence the printed "Server says" part. It seems as though an extra new line was somehow written by the server when sending the "first response" to the client which results in an empty string being read on the second iteration. It would be worth inspecting/debugging the value of the
userOut
in the second case.Also, I'm not sure whether it's intentional but I see a empty line in case of the server output. Assuming it's the posted code snippet doing all the outputting, where is that extra newline coming from?
查看您的输出,
SERVER_RESPONSE_LOGCALC_OK
中似乎有一个额外的换行符,因为System.out.println("Message sent: " + SERVER_RESPONSE_LOGCALC_OK);
有一个额外的行在服务器输出中的后面。我会删除多余的换行符或使用简单的out.print(SERVER_RESPONSE_LOGCALC_OK)
而不是out.println(SERVER_RESPONSE_LOGCALC_OK);
。这应该可以解决您的跳过问题。您遇到的潜在缺陷是因为您一次只读取一行,然后等待用户输入后再读取另一行,而不是在等待用户输入之前尽可能多的可用行。我敢打赌,如果您在客户端中执行类似以下代码的操作,您将看到不同的输出。
Looking at your output, it appears that you have an extra linebreak in
SERVER_RESPONSE_LOGCALC_OK
, sinceSystem.out.println("Message sent: " + SERVER_RESPONSE_LOGCALC_OK);
has an extra line after it in the server output. I'd remove the extra newline or use a simpleout.print(SERVER_RESPONSE_LOGCALC_OK)
instead ofout.println(SERVER_RESPONSE_LOGCALC_OK);
. This should solve your skipping problem.The underlying flaw you're experiencing is because you only read one line at a time and then wait for user input before reading another line instead of as many lines as are available before waiting for user input. I'm betting that if you do something like the code below in the client you will see different output.
仅按 \r 或仅按 \n 扫描 \r\n。
我认为,服务器可能会通过 \n\r-换行符回答,在这种情况下,您将成为双换行符(一个用于 \n,另一个用于 \r)。
据我所知,这对于您的代码功能来说可能不是问题。
因此,要跳过日志记录问题:您可以检查空的 userOut (通过空性检查)。
scans for \r\n, by \r only or by \n only.
i think, the server may answer by \n\r-linebreak, in this case youll become your double-linebreak (one for \n, anotherone for \r).
As far as i see, this might not be an problem to the functionality of your code.
so, to skip the logging-problem: you can check for empty userOut (check by emptyness).