@@ -248,6 +248,36 @@ impl UdpSocket {
248248 self . sys . recv_from ( buf)
249249 }
250250
251+ /// Receives data from the socket, without removing it from the input queue.
252+ /// On success, returns the number of bytes read and the address from whence
253+ /// the data came.
254+ /// Receives data from the socket. On success, returns the number of bytes
255+ /// read and the address from whence the data came.
256+ ///
257+ /// # Examples
258+ ///
259+ /// ```no_run
260+ /// # use std::error::Error;
261+ /// #
262+ /// # fn main() -> Result<(), Box<dyn Error>> {
263+ /// use mio::net::UdpSocket;
264+ ///
265+ /// let socket = UdpSocket::bind("127.0.0.1:0".parse()?)?;
266+ ///
267+ /// // We must check if the socket is readable before calling recv_from,
268+ /// // or we could run into a WouldBlock error.
269+ ///
270+ /// let mut buf = [0; 9];
271+ /// let (num_recv, from_addr) = socket.peek_from(&mut buf)?;
272+ /// println!("Received {:?} -> {:?} bytes from {:?}", buf, num_recv, from_addr);
273+ /// #
274+ /// # Ok(())
275+ /// # }
276+ /// ```
277+ pub fn peek_from ( & self , buf : & mut [ u8 ] ) -> io:: Result < ( usize , SocketAddr ) > {
278+ self . sys . peek_from ( buf)
279+ }
280+
251281 /// Sends data on the socket to the address previously bound via connect(). On success,
252282 /// returns the number of bytes written.
253283 pub fn send ( & self , buf : & [ u8 ] ) -> io:: Result < usize > {
@@ -260,6 +290,12 @@ impl UdpSocket {
260290 self . sys . recv ( buf)
261291 }
262292
293+ /// Receives data from the socket, without removing it from the input queue.
294+ /// On success, returns the number of bytes read.
295+ pub fn peek ( & self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
296+ self . sys . peek ( buf)
297+ }
298+
263299 /// Connects the UDP socket setting the default destination for `send()`
264300 /// and limiting packets that are read via `recv` from the address specified
265301 /// in `addr`.
0 commit comments