1414//! The destination and binding addresses can either be an IPv4 or IPv6
1515//! address. There is no corresponding notion of a server because UDP is a
1616//! datagram protocol.
17- //!
18- //! A UDP connection implements the `Reader` and `Writer` traits.
1917
2018use clone:: Clone ;
2119use result:: { Ok , Err } ;
@@ -24,6 +22,36 @@ use io::{Reader, Writer, IoResult};
2422use rt:: rtio:: { RtioSocket , RtioUdpSocket , IoFactory , LocalIo } ;
2523
2624/// A User Datagram Protocol socket.
25+ ///
26+ /// This is an implementation of a bound UDP socket. This supports both IPv4 and
27+ /// IPv6 addresses, and there is no corresponding notion of a server because UDP
28+ /// is a datagram protocol.
29+ ///
30+ /// # Example
31+ ///
32+ /// ```rust,no_run
33+ /// # #[allow(unused_must_use)];
34+ /// use std::io::net::udp::UdpSocket;
35+ /// use std::io::net::ip::{Ipv4Addr, SocketAddr};
36+ ///
37+ /// let addr = SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 34254 };
38+ /// let mut socket = match UdpSocket::bind(addr) {
39+ /// Ok(s) => s,
40+ /// Err(e) => fail!("couldn't bind socket: {}", e),
41+ /// };
42+ ///
43+ /// let mut buf = [0, ..10];
44+ /// match socket.recvfrom(buf) {
45+ /// Ok((amt, src)) => {
46+ /// // Send a reply to the socket we received data from
47+ /// let buf = buf.mut_slice_to(amt);
48+ /// buf.reverse();
49+ /// socket.sendto(buf, src);
50+ /// }
51+ /// Err(e) => println!("couldn't receive a datagram: {}", e)
52+ /// }
53+ /// drop(socket); // close the socket
54+ /// ```
2755pub struct UdpSocket {
2856 priv obj: ~RtioUdpSocket
2957}
0 commit comments