繁体   English   中英

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

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

我正在使用此代码:

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

它成功地将数据发送到服务器 - 服务器成功发送数据:

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

但是由于某种原因,应用程序似乎暂停在inFromServer.readLine()方法中。 我看到了“收到了!” de-bug消息,但不是“FROM SERVER”de-bug消息。

在此输入图像描述

根本没有错误。 它似乎挂在那里。

它为什么悬挂,我该如何解决?

那么这只是意味着inFromServer没有收到任何行。

确保你真的送了一条线,

读一行文字。 一条线被认为是由换行符('\\ n'),回车符('\\ r')或回车符中的任何一个终止,后面紧跟换行符。

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

请注意,这个接收一个布尔值,但是调用readLine只是在传递false调用它,除非在Linux上。

注意for(;;)循环,这是一个无限循环。

尝试连接从服务器发送的“行”

System.getProperty("line.separator");

暂无
暂无

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

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