简体   繁体   中英

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:

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. 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.

The messages are being sent from my server and they have "\\r\\n" as their last two chars.

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.

On a side note, running probably ought to be marked as volatile.

如果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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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