UDP 客户端/服务器 ....包括 16 位消息序列号,用于过滤重复项
我的作业包括使用UDP服务发送图像文件(使用我成功实现的java)。我的教授要求包括:
“交换的数据消息还必须有一个头部分,以便发送方包含16位消息序列号,以便接收端进行重复过滤”
如何做到这一点?
my assignment includes sending an image file using UDP service (using java I implemented that successfully). My professor asked to include:
"The exchanged data messages must also have a header part for the sender to include 16-bit message sequence number for duplicate filtering at the receiver end"
How to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设要创建 UDP 数据包,您将使用 ByteArrayOutputStream 来生成数据。如果是这种情况,只需在该 ByteArrayOutputStream 之上包装一个 DataOutputStream,并在将图像数据写入流之前调用 writeInt(somesequenceNumber) 即可。
在接收端,执行相反的操作,将 DataInputStream 包装在 ByteArrayInputStream 周围,然后调用 readInt() 来获取序列号。从那里您可以检查您是否已经收到该数据包。
东西
类似于Write Side
Read Side 的
,其中 sawSequenceNumbers 是某个 Set
I assume to create your UDP packet, you are using a ByteArrayOutputStream to generate the data. If that is the case, just Wrap a DataOutputStream on top of that ByteArrayOutputStream, and call writeInt(somesequenceNumber) before writing the image data to the stream.
on the receive side, do the opposite, wrap a DataInputStream around a ByteArrayInputStream, and call readInt() to get the sequence number. From there you can check whether you have already received this packet.
Something like
Write Side
Read Side
where seenSequenceNumbers is some Set
对于 16 位值,我将使用 DataOutputStream.writeShort() 和 DataInputSTream readShort()/readUnsignedShort()。 writeInt() 和 readInt() 用于 32 位值。如果您想避免重复,那么 32 位值在任何情况下都可能是更好的选择。 ;)
For a 16-bit value I would use DataOutputStream.writeShort() and DataInputSTream readShort()/readUnsignedShort(). writeInt() and readInt() are for 32-bit values. If you want to avoid duplicates, a 32-bit value may be a better choice in any case. ;)