1) The API to send here returns a Result<usize> . Why is this? In my head, UDP transmission is all or nothing. The return value seems to suggest that the sending may be successful, but the whole data cannot be written, which makes me code as follows:
let mut bytes_written = 0; while bytes_written < data.len() { bytes_written += match udp_socket.send_to(&data[bytes_written..]) { Ok(bytes_tx) => bytes_tx, Err(_) => break, } }
Recently, someone told me that this is completely unnecessary. But I do not understand. If so, then why does Result<()> return instead, what did I expect?
2) For reading , although I understand. I could give it a buffer of 100 bytes in size, but the datagram can be only 50 bytes long. So essentially I should use only read_buf[..size_read] . Here my question is, what happens if the buffer size is 100 and the datagram size is 150 bytes? Will recv_from fill only 100 bytes and return Ok(100, some_peer_addr) ? If I re-read, will it populate the remaining datagram? What if another 50 byte datagram arrived before my second read? Will I only get the remaining 50 bytes a second time and 50 bytes of a new datagram for the 3rd time, or the full 100 bytes a second time, which also contains a new datagram? Or will there be a mistake, and I will lose the first datagram in my initial reading and never be able to restore it?
udp networking rust
ustulation
source share