DataInput:通过 TCP 丢失的字节数

DataInput: Bytes lost over TCP

我正在通过

发送数据
data.writeInt()

其中 Data 是一个 DataOutput,它有一个套接字的输出流。

在另一端,我有一个套接字的 InputStream 和一个 DataInput,我通过

读取
data.readInt()

我已确认每次写入都与适当的读取相匹配。然后我用 wireshark 查看包,一切都应该在那里。

但是,当我发送 17 时,我得到 1114112。这被移动了 2 个字节,所以我是否可能在某处丢失了两个字节?

我做了一些添加 flush() 的实验,但问题仍然存在。此外,我还没有(还)设法编写一个较小的示例来说明问题(这就是为什么我不在此处包含代码)。然而,大例子是稳定的,它每次都发生在同一个位置。

使用不同的数据,我在 17(应该是 0)之后得到 285212672。这看起来 17 的最后一个字节被读取了两次。

任何想法这可能是什么或建议如何调试它?

亚历克斯

I have confirmed that each write is matched by the appropriate read.

显然不是这样。重新检查。显然您与发件人不同步。例如,您可能在应该使用 readFully().

的地方使用 read()

NB TCP 不会丢失数据。

This is shifted by 2 bytes, so is it possible I loose two bytes somewhere?

可以肯定:'somewhere'在您的代码中。

I did some experiments with adding flush(), but the problem stayed as it was.

只在意料之中。没有理由 flush() 应该有所作为。

Also I have not (yet) managed to write a smaller example which shows the problem (which is why I do not include code here). However, the large example is stable, it happens at the same location every time.

同样,这只是意料之中的事情。您有一个应用程序协议错误。