我使用套接字在服務器和客戶端之間進行通信。 由於某些原因,客戶端會跳過服務器發送的每一行。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消息中,服務器做出了響應,但客戶端沒有收到服務器響應。
有什麼想法?
但它確實收到迴應,不是嗎?否則「服務器說:」部分不應該出現。 –