I've been using org.apache.commons.net.telnet
to connect to a control stream and send commands to the camera. I send a datastream request and open a new thread which scans an image from the camera's sensor and sends the data as raw bytes to me. I'm using standard java.io instream and outstream to read. The outstream I'm writing to a file.... just the raw bytes. However, I'm getting stuck in an infinite loop reading the data the socket sends. A instream.read()
> -1 keeps me there... I've done instream.available() > 0
, but this often cuts the image short (understandably so). I've even tried various and/ors and can never get a complete read.
I've confirmed in Wireshark that everything is passing through to my program and that a FIN is sent, but for some reason, JAVA is not picking up the FIN and giving me the -1. The oustream to the file remains open, and I never get a "complete" image scan from the sensor. (I've been manually killing the stream and reading in the image. The end goal is to toss this image into a label and use it for on the fly occasional camera exposure updates.)
Is there some way to detect the FIN message and tell the loop to kill outside of instream.read()
?
Other info: Win 7 (enterprise build), Netbeans IDE
A
instream.read() > -1
keeps me there
Of course it does. It throws away a byte and it's not a valid test in the first place. Your read loop should look like this:
int ch;
while ((ch = instream.read()) != -1)
{
// ... Cast ch to a byte and use it somehow ...
}
or this:
int count;
byte[] buffer = new byte[8192];
while ((count = instream.read(buffer)) > 0)
{
// ...
// for example:
out.write(buffer, 0, count);
}