简体   繁体   English

Java TCP FIN但没有instream eof

[英]Java TCP FIN but no instream eof

I've been using org.apache.commons.net.telnet to connect to a control stream and send commands to the camera.我一直在使用org.apache.commons.net.telnet连接到控制流并向相机发送命令。 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.我正在使用标准的 java.io 内流和外流来读取。 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). A instream.read() > -1 让我在那里...我已经完成了instream.available() > 0 ,但这通常会缩短图像(可以理解)。 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.我已经在 Wireshark 中确认一切都通过我的程序并发送了一个 FIN,但由于某种原因,JAVA 没有拿起 FIN 并给我 -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() ?有什么方法可以检测 FIN 消息并告诉循环在instream.read()之外instream.read()

Other info: Win 7 (enterprise build), Netbeans IDE其他信息:Win 7(企业版)、Netbeans IDE

A instream.read() > -1 keeps me there一个instream.read() > -1让我在那里

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);
}
  1. You really think yours stream-read-code is so special, so you can't release it?以为你的流读码很特别,不能放出来? I doubt that.我不信。

  2. .available() isn't a test for end of stream. .available()不是流结束的测试。 It just returns the amount of bytes that you can safely read without being blocked waiting for incoming data.它只返回您可以安全读取的字节数,而不会被阻止等待传入数据。

  3. What exactly you get from the stream after image data ended?图像数据结束后,您从流中得到了什么?

  4. Here's example of InputStream usage, I'm 100% sure it is valid and properly handles eof -s, exceptions and stream closing, etc.:这是InputStream用法的示例,我 100% 确定它是有效的并且可以正确处理eof -s、异常和流关闭等:

     static final int BUFFER_SIZE = 1024 * 32; public static ByteBuffer buffer(InputStream stream) throws IOException { // try (<resourse>) to properly handle 'stream.close()` try (BufferedInputStream reader = new BufferedInputStream(stream)) { byte[] buffer = new byte[BUFFER_SIZE]; ByteBuffer result = ByteBuffer.allocate(BUFFER_SIZE); int totalReaded = 0, readed; while ((readed = reader.read(buffer)) >= 0) { if (readed > result.remaining()) { // well, we've exceeded capacity of given buffer, make it grow ByteBuffer t = result; result = (ByteBuffer) ByteBuffer.allocate((int)((totalReaded + readed) * 1.3)); result.put(t.array(), 0, totalReaded); } totalReaded += readed; result.put(buffer, 0, readed); } return ByteBuffer.wrap(result.array(), 0, totalReaded); } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM