Eduardo Bautista
Eduardo Bautista

Reputation: 1428

How do I access the data sent over UDP?

Looking at the documentation, I can only find a way to return the size of the content sent over UDP:

Receives data from the socket. On success, returns the number of bytes read and the address from whence the data came.

Is there currently a way to output the content?

Upvotes: 1

Views: 181

Answers (1)

Chris Morgan
Chris Morgan

Reputation: 90902

fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>

The data is read into the start of buf. Thus, the data read can be accessed as a &[u8] like so:

match socket.recv_from(buf) {
    Ok((bytes_read, _)) => Some(&buf[0..bytes_read]),
    Err(_) => None,
}

Upvotes: 2

Related Questions