Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are datagrams always received completely?

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();

python:

class MyUDPHandler(socketserver.BaseRequestHandler):
    def handle(self):
        amount = len (self.request[0])

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?

Edit:
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?

like image 893
XZS Avatar asked Oct 28 '11 15:10

XZS


People also ask

How big is a datagram?

Unlike the HLen field, the Length field counts bytes rather than words. Thus, the maximum size of an IP datagram is 65,535 bytes.

What is a situation in a network layer in which too many datagrams are present in an area of the Internet?

Congestion control Congestion in the network layer is a situation in which too many datagrams are present bin an area of the internet. It may occur if the number of datagrams sends by a source to a destination are beyond the capacity of the network or router.

What is the difference between DatagramPacket and DatagramSocket?

DatagramPacket object is the data container. DatagramSocket is the mechanism used to send or receive the DatagramPackets.


1 Answers

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.

Edit to incorporate Will's comment which is the best kind of correct (i.e., technically):

¹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.

like image 195
Jon Avatar answered Sep 18 '22 15:09

Jon