| 1 | //! Networking primitives for TCP/UDP communication. |
| 2 | //! |
| 3 | //! This module provides networking functionality for the Transmission Control and User |
| 4 | //! Datagram Protocols, as well as types for IP and socket addresses. |
| 5 | //! |
| 6 | //! This module is an async version of [`std::net`]. |
| 7 | //! |
| 8 | //! # Organization |
| 9 | //! |
| 10 | //! * [`TcpListener`] and [`TcpStream`] provide functionality for communication over TCP |
| 11 | //! * [`UdpSocket`] provides functionality for communication over UDP |
| 12 | //! * [`IpAddr`] represents IP addresses of either IPv4 or IPv6; [`Ipv4Addr`] and |
| 13 | //! [`Ipv6Addr`] are respectively IPv4 and IPv6 addresses |
| 14 | //! * [`SocketAddr`] represents socket addresses of either IPv4 or IPv6; [`SocketAddrV4`] |
| 15 | //! and [`SocketAddrV6`] are respectively IPv4 and IPv6 socket addresses |
| 16 | //! * [`ToSocketAddrs`] is a trait that used for generic address resolution when interacting |
| 17 | //! with networking objects like [`TcpListener`], [`TcpStream`] or [`UdpSocket`] |
| 18 | //! * Other types are return or parameter types for various methods in this module |
| 19 | //! |
| 20 | //! [`IpAddr`]: enum.IpAddr.html |
| 21 | //! [`Ipv4Addr`]: struct.Ipv4Addr.html |
| 22 | //! [`Ipv6Addr`]: struct.Ipv6Addr.html |
| 23 | //! [`SocketAddr`]: enum.SocketAddr.html |
| 24 | //! [`SocketAddrV4`]: struct.SocketAddrV4.html |
| 25 | //! [`SocketAddrV6`]: struct.SocketAddrV6.html |
| 26 | //! [`TcpListener`]: struct.TcpListener.html |
| 27 | //! [`TcpStream`]: struct.TcpStream.html |
| 28 | //! [`ToSocketAddrs`]: trait.ToSocketAddrs.html |
| 29 | //! [`UdpSocket`]: struct.UdpSocket.html |
| 30 | //! |
| 31 | //! # Platform-specific extensions |
| 32 | //! |
| 33 | //! APIs such as Unix domain sockets are available on certain platforms only. You can find |
| 34 | //! platform-specific extensions in the [`async_std::os`] module. |
| 35 | //! |
| 36 | //! [`async_std::os`]: ../os/index.html |
| 37 | //! [`std::net`]: https://doc.rust-lang.org/std/net/index.html |
| 38 | //! |
| 39 | //! # Examples |
| 40 | //! |
| 41 | //! A simple UDP echo server: |
| 42 | //! |
| 43 | //! ```no_run |
| 44 | //! # fn main() -> std::io::Result<()> { async_std::task::block_on(async { |
| 45 | //! # |
| 46 | //! use async_std::net::UdpSocket; |
| 47 | //! |
| 48 | //! let socket = UdpSocket::bind("127.0.0.1:8080" ).await?; |
| 49 | //! let mut buf = vec![0u8; 1024]; |
| 50 | //! |
| 51 | //! loop { |
| 52 | //! let (n, peer) = socket.recv_from(&mut buf).await?; |
| 53 | //! socket.send_to(&buf[..n], &peer).await?; |
| 54 | //! } |
| 55 | //! # |
| 56 | //! # }) } |
| 57 | //! ``` |
| 58 | |
| 59 | pub use std::net::AddrParseError; |
| 60 | pub use std::net::Shutdown; |
| 61 | pub use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; |
| 62 | pub use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6}; |
| 63 | |
| 64 | #[cfg (not(target_os = "unknown" ))] |
| 65 | pub use addr::ToSocketAddrs; |
| 66 | #[cfg (not(target_os = "unknown" ))] |
| 67 | pub use tcp::{Incoming, TcpListener, TcpStream}; |
| 68 | #[cfg (not(target_os = "unknown" ))] |
| 69 | pub use udp::UdpSocket; |
| 70 | |
| 71 | #[cfg (not(target_os = "unknown" ))] |
| 72 | mod addr; |
| 73 | #[cfg (not(target_os = "unknown" ))] |
| 74 | mod tcp; |
| 75 | #[cfg (not(target_os = "unknown" ))] |
| 76 | mod udp; |
| 77 | |