1 //! Networking primitives for TCP/UDP communication.
3 //! This module provides networking functionality for the Transmission Control and User
4 //! Datagram Protocols, as well as types for IP and socket addresses.
8 //! * [`TcpListener`] and [`TcpStream`] provide functionality for communication over TCP
9 //! * [`UdpSocket`] provides functionality for communication over UDP
10 //! * [`IpAddr`] represents IP addresses of either IPv4 or IPv6; [`Ipv4Addr`] and
11 //! [`Ipv6Addr`] are respectively IPv4 and IPv6 addresses
12 //! * [`SocketAddr`] represents socket addresses of either IPv4 or IPv6; [`SocketAddrV4`]
13 //! and [`SocketAddrV6`] are respectively IPv4 and IPv6 socket addresses
14 //! * [`ToSocketAddrs`] is a trait that used for generic address resolution when interacting
15 //! with networking objects like [`TcpListener`], [`TcpStream`] or [`UdpSocket`]
16 //! * Other types are return or parameter types for various methods in this module
18 //! [`IpAddr`]: ../../std/net/enum.IpAddr.html
19 //! [`Ipv4Addr`]: ../../std/net/struct.Ipv4Addr.html
20 //! [`Ipv6Addr`]: ../../std/net/struct.Ipv6Addr.html
21 //! [`SocketAddr`]: ../../std/net/enum.SocketAddr.html
22 //! [`SocketAddrV4`]: ../../std/net/struct.SocketAddrV4.html
23 //! [`SocketAddrV6`]: ../../std/net/struct.SocketAddrV6.html
24 //! [`TcpListener`]: ../../std/net/struct.TcpListener.html
25 //! [`TcpStream`]: ../../std/net/struct.TcpStream.html
26 //! [`ToSocketAddrs`]: ../../std/net/trait.ToSocketAddrs.html
27 //! [`UdpSocket`]: ../../std/net/struct.UdpSocket.html
29 #![stable(feature = "rust1", since = "1.0.0")]
31 use crate::io::{self, Error, ErrorKind};
33 #[stable(feature = "rust1", since = "1.0.0")]
34 pub use self::addr::{SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs};
35 #[stable(feature = "rust1", since = "1.0.0")]
36 pub use self::ip::{IpAddr, Ipv4Addr, Ipv6Addr, Ipv6MulticastScope};
37 #[stable(feature = "rust1", since = "1.0.0")]
38 pub use self::parser::AddrParseError;
39 #[stable(feature = "rust1", since = "1.0.0")]
40 pub use self::tcp::{Incoming, TcpListener, TcpStream};
41 #[stable(feature = "rust1", since = "1.0.0")]
42 pub use self::udp::UdpSocket;
52 /// Possible values which can be passed to the [`shutdown`] method of
55 /// [`shutdown`]: struct.TcpStream.html#method.shutdown
56 /// [`TcpStream`]: struct.TcpStream.html
57 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
58 #[stable(feature = "rust1", since = "1.0.0")]
60 /// The reading portion of the [`TcpStream`] should be shut down.
62 /// All currently blocked and future [reads] will return [`Ok(0)`].
64 /// [`TcpStream`]: ../../std/net/struct.TcpStream.html
65 /// [reads]: ../../std/io/trait.Read.html
66 /// [`Ok(0)`]: ../../std/result/enum.Result.html#variant.Ok
67 #[stable(feature = "rust1", since = "1.0.0")]
69 /// The writing portion of the [`TcpStream`] should be shut down.
71 /// All currently blocked and future [writes] will return an error.
73 /// [`TcpStream`]: ../../std/net/struct.TcpStream.html
74 /// [writes]: ../../std/io/trait.Write.html
75 #[stable(feature = "rust1", since = "1.0.0")]
77 /// Both the reading and the writing portions of the [`TcpStream`] should be shut down.
79 /// See [`Shutdown::Read`] and [`Shutdown::Write`] for more information.
81 /// [`TcpStream`]: ../../std/net/struct.TcpStream.html
82 /// [`Shutdown::Read`]: #variant.Read
83 /// [`Shutdown::Write`]: #variant.Write
84 #[stable(feature = "rust1", since = "1.0.0")]
89 const fn htons(i: u16) -> u16 {
93 const fn ntohs(i: u16) -> u16 {
97 fn each_addr<A: ToSocketAddrs, F, T>(addr: A, mut f: F) -> io::Result<T>
99 F: FnMut(io::Result<&SocketAddr>) -> io::Result<T>,
101 let addrs = match addr.to_socket_addrs() {
103 Err(e) => return f(Err(e)),
105 let mut last_err = None;
108 Ok(l) => return Ok(l),
109 Err(e) => last_err = Some(e),
112 Err(last_err.unwrap_or_else(|| {
113 Error::new(ErrorKind::InvalidInput, "could not resolve to any addresses")