tags:

views:

206

answers:

3

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.

+3  A: 

Is the end-of-line character being transmitted from the server, i.e. 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.

Rob
Flushing the output stream server side fixed it thanks.
Omar Kooheji
The end-of-line cause a flush is peculiarity of *some* PrintStream methods (depending on how it feels). PrintWriter is differently odd. Use fluch when you mean it.
Tom Hawtin - tackline
+2  A: 

If an Exception is raised by readLine, nothing is logged.

erickson
A: 

There isn't enough information. At a guess you are not flushing the output buffers.

Note also, you haven't specified character encoding.

Tom Hawtin - tackline