When available() returns 0 all it means is that there aren't any bytes available
right now. That might be because there's a tiny delay in the network and more bytes will be available within milliseconds - or it might mean that no bytes are available yet because they haven't been sent from the other end of the stream yet. Maybe we're waiting for a user to press a button or something. Or maybe there's some sort of networking error and the stream has gotten somhow disconnected from the source (but hasn't thrown an error yet sinc ethe problem hasn't been detected.) Ultimately, the available() method is pretty useless for figureing out what's wrong.
I would suggest that if your method is still blocking with the latest code shown above,
you should look at what's happening at the other end of the socket. Has the server sent all the information it needs to? Is the socket still open? Quite possibly, the server has sent a complete message, but the socket is still open because the server is still listening for input from you. If you send another signal to the server, the server will respond again using the same socket. So in between messages, your program is blocking because it's listening to the server, and the server doesn't have anything else to say.
I'd recommend printing out everything that you receive from the socket, as it arrives, so you can figure out where in the message you are. You may find that you need to put the socket reader in a separate
thread so that it keeps listening to the server, but your program can do other things while it's waiting.
Indicentally, your socket reading can be made much more efficient if you read bytes in bulk rather than one at a time. Also, if you're converting bytes to chars you'd be better off using an InputStreamReader so you can control character encoding easily.
If the server responds in
test messages, and if it always ends messsages with a newline, then you might also benefit from a BufferedReader: