Java client server sending bytes receiver listens indefinitely
- by Rob
Hello,
I'm trying to write a Java program for Windows that involves communication with a server program located on a foreign machine.My program successfully connects to the server, successfully writes a byte array to it, and waits for a response.
I know that the server is printing bytes (the response) back to me one byte at a time. I've tried using a DataInputStream object with various methods (read, readByte etc.), I've tried using a BufferedReader object with its methods (read, readLine etc.) but all the reader objects and various methods that I've used all come up against the same problem.
The bytes are being successfully read (each time a byte or bytes are read, I can print them to the console, and they are what I'd expect them to be). The problem is that my reader doesn't know when to stop reading. Even if the server has sent all its bytes, the reader function on my end waits for more data, indefinitely, and so the program hangs at the read function.
This problem seems to affect all the techniques that I have tried. I've been running tests with a simple client program and server program, each about 40 or 50 lines long, where the client connects to the server, and sends some bytes to it. All the techniques I've tried for the server reader result in the same problem mentioned above (the server hangs waiting for more input from the client, even though it has sent all its data).
I'm really desperate for some help on this. It's important that I get this program finished soon, and it's basically complete except for this communication issue. Any help is much appreciated!
-Rob