Datagramsocket:接收(...)如何处理数据包的碎片 [英] Datagramsocket: how receive(...) handles fragmentation of a packet

查看:93
本文介绍了Datagramsocket:接收(...)如何处理数据包的碎片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从我的教授那里得知,使用UDP套接字发送的数据报包在较低层中被分段,可能在接收端以多个包的形式到达。例如,如果我在数据报包中发送1000字节数据,则在接收端可能到达,比如说2个字节,500个字节,12个字节,依此类推。因此,他建议多次接收(...)接收发送方发送的整个1000字节的数据包。

I came to know from my Professor that, a datagram packet sent using UDP socket gets fragmented in the lower layers and may arrive as multiple packets at the receiver end. For e.g, if I send a 1000 bytes data in a datagram packet, at the receiving end it might arrive as, say 2 bytes, 500 bytes, 12 bytes, and so on. Therefore, he suggested to do multiple receive(...) to receive the entire 1000 byte packet sent by the sender.

稍后当我浏览数据报套接字接收(...)的Java文档时,有一行如下所示:此方法一直阻塞,直到数据报为止收到。...这是否意味着当我们使用Java时,收到整个数据报包并且不需要进行多次接收(即使理论上是这种情况)?

Later when I went through the Java documentation for datagram socket receive(...) and there is a line that reads as follows: "This method blocks until a datagram is received. ..." Does it mean that entire datagram packet is received and don't need to do multiple receive (even though it's the case in theory) when we use Java?

PLS。澄清。如果每个数据包的多次接收(...)是解决此问题的唯一选择,请参阅。提供有关如何执行此操作的建议。

Pls. clarify. If multiple receive(...) for each packet is the only option to get around this problem, pls. give suggestions on how to do this.

推荐答案

的任何调用receive()会给你一个完整的数据包 - 片段处理发生在套接字下面的两层。碎片和碎片整理发生在网络/互联网层( IP ),因此套接字永远不会看到片段但只接收完整和完整的UDP / TCP数据包(只有完整的数据包被发送到侦听端口)。

Any call to receive() will give you an entire packet - the fragment handling happens in two layers below the socket. The fragmentation and defragmentation happens in the Network/Internet layer (IP), so the socket will never see the fragments but only receive entire and full UDP/TCP packets (only full packets gets sent to the listening port).

所以,不,你不需要多个 receive()来获得一个数据包,但你应该是意识到UDP不可靠,所以如果一个片段在网络层丢失(在某些情况下,如果它无法到达),你将无法获得该数据包。

So, no, you do not need multiple receive() to get a single packet, but you should be aware that UDP is not reliable so if one fragment gets lost in the Network layer (and in some cases if it arrives out of order), you won't be able to get the packet.

您可能还想检查方法 getReceiveBufferSize() setReceiveBufferSize() 如果您在接收数据包时遇到问题 - 如果缓冲区大小小于数据包大小,则无法保证您可以接收数据包。

You might also want to check the methods getReceiveBufferSize() and setReceiveBufferSize() if you're having trouble receiving packets - if the buffer size is smaller than the packet size, it's not guaranteed that you can receive the packet.

这篇关于Datagramsocket:接收(...)如何处理数据包的碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆