简体   繁体   English

为什么此缓冲的读取器不接收任何数据?

[英]Why is this buffered reader not receiving any data?

I have a thread that is supposed to listen for acknowledgements of messages and process them, but it seems that the thread is never reciving the acknowledgements. 我有一个线程应该侦听消息的确认并对其进行处理,但是似乎该线程从未接收到确认。

Here is the relevant code in the thread: 这是线程中的相关代码:

private class TcpReader extends Thread {

    BufferedReader reader;
    boolean running = false;
    public TcpReader(BufferedReader reader)
    {
        this.reader = reader;
    }

    public void run() {
        running = true;

        while (running) {
            try {
                String line = reader.readLine();
                logger.debug("TCP READER RECIEVED MESSAGE ["+ line + "]");
                // Do stuff
            } catch (Exception e) {
            }
        }
    }
}

The thread is created in the following code which happens in a different class: 该线程是通过以下代码创建的,该代码发生在不同的类中:

sock = new Socket(hostName, port);
out = new PrintWriter(sock.getOutputStream(), true);
isConnected = true;
BufferedReader reader =
        new BufferedReader(new InputStreamReader(sock.getInputStream()));
tcpReader = new TcpReader(reader);
tcpReader.start();

The Variables are instantiated in the as member variables: 变量在as成员变量中实例化:

Socket sock;
PrintWriter out;
BufferedReader in;

The flow of execution is my server class recives a message and then replies with an ACK that the client is supposed to pick up on. 执行流程是我的服务器类接收一条消息,然后用客户端应该接受的ACK进行答复。 However since the client is not to wait for the ack to continue A separate thread waits for all responses. 但是,由于客户端不等待确认继续,因此单独的线程等待所有响应。

The readline code never reads anything. readline代码从不读取任何内容。

The messages are being sent from my server and they have "\\r\\n" as their last two chars. 邮件是从我的服务器发送的,最后两个字符为“ \\ r \\ n”。

Is the end-of-line character being transmitted from the server, ie is it flushing the output stream at the other end? 是否从服务器传输了行尾字符,即它是否刷新了另一端的输出流? You could use telnet or a simple packet sniffer to check what's being sent. 您可以使用telnet或简单的数据包嗅探器检查正在发送的内容。

On a side note, running probably ought to be marked as volatile. 附带说明,可能应该将running标记为不稳定。

如果readLine引发Exception ,则不会记录任何内容。

There isn't enough information. 没有足够的信息。 At a guess you are not flushing the output buffers. 猜测您没有刷新输出缓冲区。

Note also, you haven't specified character encoding. 另请注意,您尚未指定字符编码。

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

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