@@ -206,6 +206,29 @@ impl UdpSocket {
206
206
self . watcher . recv_from ( buf) . await
207
207
}
208
208
209
+ /// Receives data from socket without removing it from the queue.
210
+ ///
211
+ /// On success, returns the number of bytes peeked and the origin.
212
+ ///
213
+ /// # Examples
214
+ ///
215
+ /// ```no_run
216
+ /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
217
+ /// #
218
+ /// use async_std::net::UdpSocket;
219
+ ///
220
+ /// let socket = UdpSocket::bind("127.0.0.1:0").await?;
221
+ ///
222
+ /// let mut buf = vec![0; 1024];
223
+ /// let (n, peer) = socket.peek_from(&mut buf).await?;
224
+ /// println!("Peeked {} bytes from {}", n, peer);
225
+ /// #
226
+ /// # Ok (()) }) }
227
+ /// ```
228
+ pub async fn peek_from ( & self , buf : & mut [ u8 ] ) -> io:: Result < ( usize , SocketAddr ) > {
229
+ self . watcher . peek_from ( buf) . await
230
+ }
231
+
209
232
/// Connects the UDP socket to a remote address.
210
233
///
211
234
/// When connected, methods [`send`] and [`recv`] will use the specified address for sending
@@ -301,6 +324,30 @@ impl UdpSocket {
301
324
self . watcher . recv ( buf) . await
302
325
}
303
326
327
+ /// Receives data from the socket without removing it from the queue.
328
+ ///
329
+ /// On success, returns the number of bytes peeked.
330
+ ///
331
+ /// # Examples
332
+ ///
333
+ /// ```no_run
334
+ /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
335
+ /// #
336
+ /// use async_std::net::UdpSocket;
337
+ ///
338
+ /// let socket = UdpSocket::bind("127.0.0.1:0").await?;
339
+ /// socket.connect("127.0.0.1:8080").await?;
340
+ ///
341
+ /// let mut buf = vec![0; 1024];
342
+ /// let n = socket.peek(&mut buf).await?;
343
+ /// println!("Peeked {} bytes", n);
344
+ /// #
345
+ /// # Ok(()) }) }
346
+ /// ```
347
+ pub async fn peek ( & self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
348
+ self . watcher . peek ( buf) . await
349
+ }
350
+
304
351
/// Gets the value of the `SO_BROADCAST` option for this socket.
305
352
///
306
353
/// For more information about this option, see [`set_broadcast`].
0 commit comments