@@ -69,14 +69,34 @@ impl UdpSocket {
69
69
/// The address type can be any implementor of [`ToSocketAddrs`] trait. See
70
70
/// its documentation for concrete examples.
71
71
///
72
+ /// If `addr` yields multiple addresses, `bind` will be attempted with
73
+ /// each of the addresses until one succeeds and returns the socket. If none
74
+ /// of the addresses succeed in creating a socket, the error returned from
75
+ /// the last attempt (the last address) is returned.
76
+ ///
72
77
/// [`ToSocketAddrs`]: ../../std/net/trait.ToSocketAddrs.html
73
78
///
74
79
/// # Examples
75
80
///
81
+ /// Create a UDP socket bound to `127.0.0.1:3400`:
82
+ ///
76
83
/// ```no_run
77
84
/// use std::net::UdpSocket;
78
85
///
79
- /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
86
+ /// let socket = UdpSocket::bind("127.0.0.1:3400").expect("couldn't bind to address");
87
+ /// ```
88
+ ///
89
+ /// Create a UDP socket bound to `127.0.0.1:3400`. If the socket cannot be
90
+ /// bound to that address, create a UDP socket bound to `127.0.0.1:3401`:
91
+ ///
92
+ /// ```no_run
93
+ /// use std::net::{SocketAddr, UdpSocket};
94
+ ///
95
+ /// let addrs = [
96
+ /// SocketAddr::from(([127, 0, 0, 1], 3400)),
97
+ /// SocketAddr::from(([127, 0, 0, 1], 3401)),
98
+ /// ];
99
+ /// let socket = UdpSocket::bind(&addrs[..]).expect("couldn't bind to address");
80
100
/// ```
81
101
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
82
102
pub fn bind < A : ToSocketAddrs > ( addr : A ) -> io:: Result < UdpSocket > {
@@ -130,6 +150,9 @@ impl UdpSocket {
130
150
/// Address type can be any implementor of [`ToSocketAddrs`] trait. See its
131
151
/// documentation for concrete examples.
132
152
///
153
+ /// It is possible for `addr` to yield multiple addresses, but `send_to`
154
+ /// will only send data to the first address yielded by `addr`.
155
+ ///
133
156
/// This will return an error when the IP version of the local socket
134
157
/// does not match that returned from [`ToSocketAddrs`].
135
158
///
@@ -562,14 +585,37 @@ impl UdpSocket {
562
585
/// `recv` syscalls to be used to send data and also applies filters to only
563
586
/// receive data from the specified address.
564
587
///
588
+ /// If `addr` yields multiple addresses, `connect` will be attempted with
589
+ /// each of the addresses until a connection is successful. If none of
590
+ /// the addresses are able to be connected, the error returned from the
591
+ /// last connection attempt (the last address) is returned.
592
+ ///
565
593
/// # Examples
566
594
///
595
+ /// Create a UDP socket bound to `127.0.0.1:3400` and connect the socket to
596
+ /// `127.0.0.1:8080`:
597
+ ///
567
598
/// ```no_run
568
599
/// use std::net::UdpSocket;
569
600
///
570
- /// let socket = UdpSocket::bind("127.0.0.1:34254 ").expect("couldn't bind to address");
601
+ /// let socket = UdpSocket::bind("127.0.0.1:3400 ").expect("couldn't bind to address");
571
602
/// socket.connect("127.0.0.1:8080").expect("connect function failed");
572
603
/// ```
604
+ ///
605
+ /// Create a UDP socket bound to `127.0.0.1:3400` and connect the socket to
606
+ /// `127.0.0.1:8080`. If that connection fails, then the UDP socket will
607
+ /// connect to `127.0.0.1:8081`:
608
+ ///
609
+ /// ```no_run
610
+ /// use std::net::{SocketAddr, UdpSocket};
611
+ ///
612
+ /// let socket = UdpSocket::bind("127.0.0.1:3400").expect("couldn't bind to address");
613
+ /// let connect_addrs = [
614
+ /// SocketAddr::from(([127, 0, 0, 1], 8080)),
615
+ /// SocketAddr::from(([127, 0, 0, 1], 8081)),
616
+ /// ];
617
+ /// socket.connect(&connect_addrs[..]).expect("connect function failed");
618
+ /// ```
573
619
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
574
620
pub fn connect < A : ToSocketAddrs > ( & self , addr : A ) -> io:: Result < ( ) > {
575
621
super :: each_addr ( addr, |addr| self . 0 . connect ( addr) )
0 commit comments