数据包是否总是完全接收? [英] Are datagrams always received completely?
问题描述
大多数数据报接收功能如c的接收或读,Java类的DatagramPacket类或蟒SocketServer的,包括找出接收的数据的量的可能性。
Most datagram receiving functions such as c's recv or read, javas DatagramPacket class or pythons SocketServer, include the possibility to find out the amount of received data.
C:
int amount = recv(sock, buf, n, MSG_WAITALL);
Java的:
int amount = datagramSocket.getLength();
蟒蛇:
class MyUDPHandler(socketserver.BaseRequestHandler):
def handle(self):
amount = len (self.request[0])
这些是可靠的?或者是有可能的消息只有部分被接收,例如由于数据包碎片或网络延迟?结果
换句话说:当我通过UDP发送的数据的可变长度的块,而在另一端接收它,是这些量的值正好等于原始块的大小
Are these reliable? Or is it possible that only parts of the message are received, due to for example packet fragmentation or network delay?
In other words: When I send a variable length chunk of data via udp and receive it at the other end, are these amount values exactly equal to the size of the original chunk?
编辑:结果
ninjalj取得了良好的点,我想在这里包含它。当接收功能是由一个信号中断,比如,会发生什么?当两个线程同时尝试从同一个套接字接收会发生什么?
ninjalj made a good point and I want to include it here. What happens when the receiving function is interrupted, for instance by a signal? What happens when two threads simultaneously try to receive from the same socket?
推荐答案
UDP数据报不能部分delivered¹;在交付原样或根本没有。所以,是的,你可以肯定的是完全按照你看到它在接收器的终端接收到的数据包被发送。
UDP datagrams cannot be partially delivered¹; they are delivered as-is or not at all. So yes, you can be sure that the received datagram was sent exactly as you see it on the receiver's end.
的编辑纳入威尔的评论这是最好的一种正确的(即,在技术上):的
¹They可以在IP层进行分片,但在接收端的网络堆栈要么完全重组一个数据报,并把它传递给听的过程中为发送,否则将不承认,在所有的任何数据已收到。
¹They can be fragmented at the IP level, but the network stack on the receiver side will either fully reassemble a datagram and pass it to the listening process as sent, or will not acknowledge that any data at all has been received.
这篇关于数据包是否总是完全接收?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!