readLine每隔第二行跳过一次

readLine每隔第二行跳过一次

本文介绍了BufferedReader readLine每隔第二行跳过一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用套接字在服务器和客户端之间进行通信。
不过,由于某些原因,客户端会跳过服务器发送的第二行。

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.

客户端代码:

    ...

    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);
        }
        ...

使用此显示的示例(客户端优先):

An example of using this displays (client first):

 LOGCALC
 Server says: LOGCALC: OK
 LOGCALC
 Server says:

服务器:

Client says: LOGCALC
Message sent: LOGCALC: OK

Client says: LOGCALC
Message sent: LOGCALC: OK

希望您能看到在发送给服务器的第二条LOGCALC消息中,服务器已响应,但是客户端未收到服务器响应。

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.

有什么想法吗?

推荐答案

看看您的输出,看来您在 SERVER_RESPONSE_LOGCALC_OK 中有一个额外的换行符,因为 System.out.println(已发送消息: + 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, since System.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 simple out.print(SERVER_RESPONSE_LOGCALC_OK) instead of out.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.

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

        if(!in.available()){
          userIn = console.readLine();                //Gets user input
          out.println(userIn);                        //Sends user input to server
        }
    }

这篇关于BufferedReader readLine每隔第二行跳过一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 04:55