@@ -48,11 +48,12 @@ use time::Duration;
48
48
/// {
49
49
/// let mut socket = UdpSocket::bind("127.0.0.1:34254")?;
50
50
///
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.
52
53
/// let mut buf = [0; 10];
53
54
/// let (amt, src) = socket.recv_from(&mut buf)?;
54
55
///
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.
56
57
/// let buf = &mut buf[..amt];
57
58
/// buf.reverse();
58
59
/// socket.send_to(buf, &src)?;
@@ -103,8 +104,12 @@ impl UdpSocket {
103
104
super :: each_addr ( addr, net_imp:: UdpSocket :: bind) . map ( UdpSocket )
104
105
}
105
106
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.
108
113
///
109
114
/// # Examples
110
115
///
@@ -115,19 +120,25 @@ impl UdpSocket {
115
120
/// let mut buf = [0; 10];
116
121
/// let (number_of_bytes, src_addr) = socket.recv_from(&mut buf)
117
122
/// .expect("Didn't receive data");
123
+ /// let filled_buf = &mut buf[..number_of_bytes];
118
124
/// ```
119
125
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
120
126
pub fn recv_from ( & self , buf : & mut [ u8 ] ) -> io:: Result < ( usize , SocketAddr ) > {
121
127
self . 0 . recv_from ( buf)
122
128
}
123
129
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.
125
136
///
126
137
/// Successive calls return the same data. This is accomplished by passing
127
138
/// `MSG_PEEK` as a flag to the underlying `recvfrom` system call.
128
139
///
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 .
131
142
///
132
143
/// # Examples
133
144
///
@@ -138,6 +149,7 @@ impl UdpSocket {
138
149
/// let mut buf = [0; 10];
139
150
/// let (number_of_bytes, src_addr) = socket.peek_from(&mut buf)
140
151
/// .expect("Didn't receive data");
152
+ /// let filled_buf = &mut buf[..number_of_bytes];
141
153
/// ```
142
154
#[ stable( feature = "peek" , since = "1.18.0" ) ]
143
155
pub fn peek_from ( & self , buf : & mut [ u8 ] ) -> io:: Result < ( usize , SocketAddr ) > {
@@ -642,8 +654,12 @@ impl UdpSocket {
642
654
self . 0 . send ( buf)
643
655
}
644
656
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.
647
663
///
648
664
/// The [`connect`] method will connect this socket to a remote address. This
649
665
/// method will fail if the socket is not connected.
@@ -659,7 +675,7 @@ impl UdpSocket {
659
675
/// socket.connect("127.0.0.1:8080").expect("connect function failed");
660
676
/// let mut buf = [0; 10];
661
677
/// match socket.recv(&mut buf) {
662
- /// Ok(received) => println!("received {} bytes", received),
678
+ /// Ok(received) => println!("received {} bytes {:?} ", received, &buf[..received] ),
663
679
/// Err(e) => println!("recv function failed: {:?}", e),
664
680
/// }
665
681
/// ```
@@ -668,13 +684,25 @@ impl UdpSocket {
668
684
self . 0 . recv ( buf)
669
685
}
670
686
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.
674
694
///
675
695
/// Successive calls return the same data. This is accomplished by passing
676
696
/// `MSG_PEEK` as a flag to the underlying `recv` system call.
677
697
///
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
+ ///
678
706
/// # Errors
679
707
///
680
708
/// This method will fail if the socket is not connected. The `connect` method
0 commit comments