I am working with an electronic device that receives commands and sends ASCII data to the client using the socket functionality in Java. I have no problems using PrintStream sending commands or using BufferedReader to receive data from the electronic device separately. The problem I am currently having is receiving data from the electronic device immeditately after I send it a command string. For example there is a command string that I can send to the device and by doing so the device will return the settings in ASCII data.

Everytime I try it I get no return data through BufferedReader. I know it works because I have tested in Hyperterminal and it works everytime.

Any help is appreciated.

Thanks in advanced

March 28th, 2011, 06:20 PM

helloworld922

Re: How to use PrintSteam and BufferedReader Simultaneously

I'm not completely positive if this will fix your problem, but I know that sometimes you have to manually flush the stream. You could give that a go and see if it works.

You can check to see if your program is actually sending something out the socket by using a profiling program (I recommend WireShark, it's a very nice open-source network monitor). Conversely, you can also check to see if you're receiving anything back.

March 29th, 2011, 07:30 AM

Mr.777

Re: How to use PrintSteam and BufferedReader Simultaneously

Each time you send the message from your device is submitted successfully, but you try to read it as fast as it's not still flushed. Use flush() to clear your output memory buffer and force it to write over the destination.
Remember, flush() will throw IO exception, handle it and i don't think if outputstream or bufferedstream will allow flush() as documentation says, they both don't work.
Anyways, give it a try, may be it will work.