简体   繁体   English

BufferedReader.readLine()暂停我的应用程序?

[英]BufferedReader.readLine() pauses my application?

I am using this code: 我正在使用此代码:

while (true) {
    sendData("hi");
    System.out.println("Data sent!");
    BufferedReader inFromServer;
    try {
        inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    } catch (IOException e1) {
        inFromServer = null;
        e1.printStackTrace();
    }
    System.out.println("Recieved!"); //I see this de-bug message.
    try {
        modifiedSentence = inFromServer.readLine();
        System.out.println("FROM SERVER: " + modifiedSentence); //I do NOT see this de-bug message!
    } catch (IOException e) {
        e.printStackTrace();
    }
}

It successfully sends data to a server - And the server successfully sends data back: 它成功地将数据发送到服务器 - 服务器成功发送数据:

public void run () {
    //handle the session using the socket (example)
    try {
        sendData("Hi");
        System.out.println("Data sent!"); //I see this de-bug message.
    } catch (Exception e) {
        e.printStackTrace();
    }
}

However for some reason, the application seems to pause at the inFromServer.readLine() method. 但是由于某种原因,应用程序似乎暂停在inFromServer.readLine()方法中。 I see the "Recieved!" 我看到了“收到了!” de-bug message, but not the "FROM SERVER" de-bug message. de-bug消息,但不是“FROM SERVER”de-bug消息。

在此输入图像描述

There are no errors at all. 根本没有错误。 It just seems to hang there. 它似乎挂在那里。

Why is it hanging, and how can I fix that? 它为什么悬挂,我该如何解决?

Well this simply means that inFromServer does not receive any line. 那么这只是意味着inFromServer没有收到任何行。

Make sure you really send a line, 确保你真的送了一条线,

Reads a line of text. 读一行文字。 A line is considered to be terminated by any one of a line feed ('\\n'), a carriage return ('\\r'), or a carriage return followed immediately by a linefeed. 一条线被认为是由换行符('\\ n'),回车符('\\ r')或回车符中的任何一个终止,后面紧跟换行符。

Have a look at the readLine method : 看看readLine方法:

String readLine(boolean ignoreLF) throws IOException {
    StringBuffer s = null;
    int startChar;

    synchronized (lock) {
        ensureOpen();
        boolean omitLF = ignoreLF || skipLF;

    bufferLoop:
        for (;;) {

            if (nextChar >= nChars)
                fill();
            if (nextChar >= nChars) { /* EOF */
                if (s != null && s.length() > 0)
                    return s.toString();
                else
                    return null;
            }
            boolean eol = false;
            char c = 0;
            int i;

            /* Skip a leftover '\n', if necessary */
            if (omitLF && (cb[nextChar] == '\n'))
                nextChar++;
            skipLF = false;
            omitLF = false;

        charLoop:
            for (i = nextChar; i < nChars; i++) {
                c = cb[i];
                if ((c == '\n') || (c == '\r')) {
                    eol = true;
                    break charLoop;
                }
            }

            startChar = nextChar;
            nextChar = i;

            if (eol) {
                String str;
                if (s == null) {
                    str = new String(cb, startChar, i - startChar);
                } else {
                    s.append(cb, startChar, i - startChar);
                    str = s.toString();
                }
                nextChar++;
                if (c == '\r') {
                    skipLF = true;
                }
                return str;
            }

            if (s == null)
                s = new StringBuffer(defaultExpectedLineLength);
            s.append(cb, startChar, i - startChar);
        }
    }
}

Note that this one receive a boolean, but calling readLine simply call this one with false passed, unless on Linux. 请注意,这个接收一个布尔值,但是调用readLine只是在传递false调用它,除非在Linux上。

Notice the for(;;) loop, which is an infinite loop. 注意for(;;)循环,这是一个无限循环。

Try concatening to the "line" sent from the server 尝试连接从服务器发送的“行”

System.getProperty("line.separator");

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

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