@@ -48,11 +48,12 @@ use time::Duration;
4848/// {
4949/// let mut socket = UdpSocket::bind("127.0.0.1:34254")?;
5050///
51- /// // read from the socket
51+ /// // Receives a single datagram message on the socket. If `buf` is too small to hold
52+ /// // the message, it will be cut off.
5253/// let mut buf = [0; 10];
5354/// let (amt, src) = socket.recv_from(&mut buf)?;
5455///
55- /// // send a reply to the socket we received data from
56+ /// // Redeclare `buf` as slice of the received data and send reverse data back to origin.
5657/// let buf = &mut buf[..amt];
5758/// buf.reverse();
5859/// socket.send_to(buf, &src)?;
@@ -103,8 +104,12 @@ impl UdpSocket {
103104 super :: each_addr ( addr, net_imp:: UdpSocket :: bind) . map ( UdpSocket )
104105 }
105106
106- /// Receives data from the socket. On success, returns the number of bytes
107- /// read and the address from whence the data came.
107+ /// Receives a single datagram message on the socket. On success, returns the number
108+ /// of bytes read and the origin.
109+ ///
110+ /// The function must be called with valid byte array `buf` of sufficient size to
111+ /// hold the message bytes. If a message is too long to fit in the supplied buffer,
112+ /// excess bytes may be discarded.
108113 ///
109114 /// # Examples
110115 ///
@@ -115,19 +120,25 @@ impl UdpSocket {
115120 /// let mut buf = [0; 10];
116121 /// let (number_of_bytes, src_addr) = socket.recv_from(&mut buf)
117122 /// .expect("Didn't receive data");
123+ /// let filled_buf = &mut buf[..number_of_bytes];
118124 /// ```
119125 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
120126 pub fn recv_from ( & self , buf : & mut [ u8 ] ) -> io:: Result < ( usize , SocketAddr ) > {
121127 self . 0 . recv_from ( buf)
122128 }
123129
124- /// Receives data from the socket, without removing it from the queue.
130+ /// Receives a single datagram message on the socket, without removing it from the
131+ /// queue. On success, returns the number of bytes read and the origin.
132+ ///
133+ /// The function must be called with valid byte array `buf` of sufficient size to
134+ /// hold the message bytes. If a message is too long to fit in the supplied buffer,
135+ /// excess bytes may be discarded.
125136 ///
126137 /// Successive calls return the same data. This is accomplished by passing
127138 /// `MSG_PEEK` as a flag to the underlying `recvfrom` system call.
128139 ///
129- /// On success, returns the number of bytes peeked and the address from
130- /// whence the data came .
140+ /// Do not use this function to implement busy waiting, instead use `libc::poll` to
141+ /// synchronize IO events on one or more sockets .
131142 ///
132143 /// # Examples
133144 ///
@@ -138,6 +149,7 @@ impl UdpSocket {
138149 /// let mut buf = [0; 10];
139150 /// let (number_of_bytes, src_addr) = socket.peek_from(&mut buf)
140151 /// .expect("Didn't receive data");
152+ /// let filled_buf = &mut buf[..number_of_bytes];
141153 /// ```
142154 #[ stable( feature = "peek" , since = "1.18.0" ) ]
143155 pub fn peek_from ( & self , buf : & mut [ u8 ] ) -> io:: Result < ( usize , SocketAddr ) > {
@@ -642,8 +654,12 @@ impl UdpSocket {
642654 self . 0 . send ( buf)
643655 }
644656
645- /// Receives data on the socket from the remote address to which it is
646- /// connected.
657+ /// Receives a single datagram message on the socket from the remote address to
658+ /// which it is connected. On success, returns the number of bytes read.
659+ ///
660+ /// The function must be called with valid byte array `buf` of sufficient size to
661+ /// hold the message bytes. If a message is too long to fit in the supplied buffer,
662+ /// excess bytes may be discarded.
647663 ///
648664 /// The [`connect`] method will connect this socket to a remote address. This
649665 /// method will fail if the socket is not connected.
@@ -659,7 +675,7 @@ impl UdpSocket {
659675 /// socket.connect("127.0.0.1:8080").expect("connect function failed");
660676 /// let mut buf = [0; 10];
661677 /// match socket.recv(&mut buf) {
662- /// Ok(received) => println!("received {} bytes", received),
678+ /// Ok(received) => println!("received {} bytes {:?} ", received, &buf[..received] ),
663679 /// Err(e) => println!("recv function failed: {:?}", e),
664680 /// }
665681 /// ```
@@ -668,13 +684,25 @@ impl UdpSocket {
668684 self . 0 . recv ( buf)
669685 }
670686
671- /// Receives data on the socket from the remote address to which it is
672- /// connected, without removing that data from the queue. On success,
673- /// returns the number of bytes peeked.
687+ /// Receives single datagram on the socket from the remote address to which it is
688+ /// connected, without removing the message from input queue. On success, returns
689+ /// the number of bytes peeked.
690+ ///
691+ /// The function must be called with valid byte array `buf` of sufficient size to
692+ /// hold the message bytes. If a message is too long to fit in the supplied buffer,
693+ /// excess bytes may be discarded.
674694 ///
675695 /// Successive calls return the same data. This is accomplished by passing
676696 /// `MSG_PEEK` as a flag to the underlying `recv` system call.
677697 ///
698+ /// Do not use this function to implement busy waiting, instead use `libc::poll` to
699+ /// synchronize IO events on one or more sockets.
700+ ///
701+ /// The [`connect`] method will connect this socket to a remote address. This
702+ /// method will fail if the socket is not connected.
703+ ///
704+ /// [`connect`]: #method.connect
705+ ///
678706 /// # Errors
679707 ///
680708 /// This method will fail if the socket is not connected. The `connect` method
0 commit comments