我正在使用套接字在服务器和客户端之间进行通信。但是出于某种原因,客户端会跳过服务器发送的每一行。
客户代码:
...
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 消息中,服务器做出了响应,但客户端没有收到服务器的响应。
有什么想法吗?