如何验证对象传输是否完成
我想通过网络从 java 实例向另一个实例发送消息(可序列化对象)。我想验证整个对象是否已正确发送。
我想我的第一步是计算对象的校验和。然后我将该校验和包含在对象中,或者为消息及其校验和构建一个容器对象。
然后我的第二步应该是根据另一侧的对象验证校验和。
我的第三步是接收者发送一条确认消息,表示已收到相关对象并且校验和已通过(或未通过)。如果我收到校验和失败警告,我会尝试重新发送几次。
过了一会儿,如果我没有收到确认,我也会尝试重新发送几次。
问题:
我的协议听起来是否适合验证对象是否已正确传输?
我还想知道我应该如何在java中实现这个?我是否使用 CRC32 类来生成校验和?
额外问题:如果我要压缩每条消息,我是否在压缩之前或之后生成校验和,以及如何在java中压缩对象?
I want to send a message (a serializable object) from a java instance to another instance over a network. I would like to verify that the whole object has been sent correctly.
I suppose my first step is to calculate the checksum of the object. Then I include that checksum in the object OR build a container object for the message and its checksum.
Then my second step should be to verify the checksum against the object on the other side.
My third step would be for the receiver to send a confirmation message saying that the object in question was received and that the checksum has passed (or not). If I receive a failed checksum warning, I try to resend it a few times.
After a little while, if I never received a confirmation, I try to resend it a few times as well.
Questions :
Does my protocol sounds right to verify that an object was transferred correctly ?
I would also like to know how am I supposed to implement this in java ? Do I use the CRC32 class to generate the checksum ?
Bonus question : If I were to compress each message, do I generate the checksum before of after the compression, and how do I compress an object in java ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您有一个相当可靠且错误率较低的网络,则不需要添加额外的校验和。我会首先在没有校验和的情况下实现您的协议,如果您确定需要它,我会添加它。
您可以使用 DeflatorOutputStream、InflatorInputStream 来压缩数据。如果压缩数据被损坏,对象在解压时很可能会抛出异常。即不太可能出现细微的错误。
但是,除非您的对象很大,否则它们可能无法很好地压缩,并且最终可能会因压缩而变得更大。
If you have a reasonably reliable network with a low error rate, you shouldn't need to add an additional checksum. I would implement your protocol without a checksum first and add if you are sure you need it.
You can compress the data with DeflatorOutputStream, InflatorInputStream. If the compressed data is corrupted the Object is highly likely to throw an exception when unpacking it. i.e. it is very unlikely to have a subtle error.
However, unless your objects are large, they may not compress very well and could endup being larger with compression.
对于压缩,我想推荐 Apache Zip Utilities:
http://commons.apache .org/compress/apidocs/org/apache/commons/compress/archivers/zip/package-summary.html
如果您正在压缩,则可以跳过校验和。压缩算法对数据损坏非常敏感。如果对象在另一端解压缩失败,那么您在传输过程中丢失了一些信息。
For compression, I would like to recommend the Apache Zip Utilities:
http://commons.apache.org/compress/apidocs/org/apache/commons/compress/archivers/zip/package-summary.html
If you are compressing then you can skip the checksum. Compression algorithms are quite sensitive to data damage. If the object fails to decompress on the other end, then you lost some information during transmission.
我同意你的步骤,但有一点补充(这就是我在 ObjectIO 流上所做的)-
[1] 将传入的内容读取为 Object,通过“instanceof”找到其类。如果它不是预期的类,则需要调试传入的内容。
在其他一些情况下,我还会发送包含有关接下来到达的对象内容的信息的字符串。解析这个字符串,读取对象,对其进行类型转换,确保它具有字符串中的信息,然后写出确认信息:)
I agree with your steps, with one addition (this is what I do, over ObjectIO streams) -
[1] Read the incoming stuff as Object, find its class with "instanceof". If it is not the expected class, time to debug what is coming in.
In some other situations, I also send out Strings that contain info about the contents of the object to arrive next. Parse this string, read the object, typecast it, make sure it has what the info in the String said, and write out the confirmation :)