E
E
Ernest Farukshin2020-02-14 19:41:57
Java
Ernest Farukshin, 2020-02-14 19:41:57

Why does in.read() wait indefinitely?

Socket socket = new Socket("localhost",2019);
InputStreamReader in = new InputStreamReader(socket.getInputStream());
char a;
String str = "";
while ((a= (char) in.read()) != -1){
    str += a;
}

I'm sending a message from a server (built on a SocketChannel). Through the debugger, I looked that the message comes in full, but after the last character has arrived, it simply stops on the line with while. I assume that this is due to the fact that the server is on the SocketChannel.
PS
I solved this problem by adding ";" after each message. , and in while I prescribe to wait until a comma. Just wondering why this is happening and maybe I'm doing something wrong.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2020-02-14
@Ernest3

char is an unsigned number and therefore will never equal -1.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question