2020-07-02 06:03:37 +00:00
|
|
|
#![deny(unsafe_op_in_unsafe_fn)]
|
2020-08-27 13:45:01 +00:00
|
|
|
|
2020-10-27 13:10:31 +00:00
|
|
|
#[cfg(all(test, not(target_os = "emscripten")))]
|
2020-08-27 13:45:01 +00:00
|
|
|
mod tests;
|
|
|
|
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::io::prelude::*;
|
2015-02-06 00:50:11 +00:00
|
|
|
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::fmt;
|
2019-04-27 15:34:08 +00:00
|
|
|
use crate::io::{self, Initializer, IoSlice, IoSliceMut};
|
2019-11-27 18:29:00 +00:00
|
|
|
use crate::net::{Shutdown, SocketAddr, ToSocketAddrs};
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::sys_common::net as net_imp;
|
|
|
|
use crate::sys_common::{AsInner, FromInner, IntoInner};
|
|
|
|
use crate::time::Duration;
|
2015-02-06 00:50:11 +00:00
|
|
|
|
2017-03-26 15:06:39 +00:00
|
|
|
/// A TCP stream between a local and a remote socket.
|
2015-02-06 00:50:11 +00:00
|
|
|
///
|
2017-03-26 15:06:39 +00:00
|
|
|
/// After creating a `TcpStream` by either [`connect`]ing to a remote host or
|
|
|
|
/// [`accept`]ing a connection on a [`TcpListener`], data can be transmitted
|
|
|
|
/// by [reading] and [writing] to it.
|
|
|
|
///
|
|
|
|
/// The connection will be closed when the value is dropped. The reading and writing
|
|
|
|
/// portions of the connection can also be shut down individually with the [`shutdown`]
|
|
|
|
/// method.
|
|
|
|
///
|
|
|
|
/// The Transmission Control Protocol is specified in [IETF RFC 793].
|
|
|
|
///
|
2020-08-15 17:17:19 +00:00
|
|
|
/// [`accept`]: TcpListener::accept
|
|
|
|
/// [`connect`]: TcpStream::connect
|
2017-03-26 15:06:39 +00:00
|
|
|
/// [IETF RFC 793]: https://tools.ietf.org/html/rfc793
|
2020-08-15 17:17:19 +00:00
|
|
|
/// [reading]: Read
|
|
|
|
/// [`shutdown`]: TcpStream::shutdown
|
|
|
|
/// [writing]: Write
|
2015-02-06 00:50:11 +00:00
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
/// # Examples
|
2015-02-06 00:50:11 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::net::TcpStream;
|
|
|
|
///
|
2018-10-11 19:35:48 +00:00
|
|
|
/// fn main() -> std::io::Result<()> {
|
2018-10-10 15:13:35 +00:00
|
|
|
/// let mut stream = TcpStream::connect("127.0.0.1:34254")?;
|
2015-02-06 00:50:11 +00:00
|
|
|
///
|
2018-10-10 15:13:35 +00:00
|
|
|
/// stream.write(&[1])?;
|
|
|
|
/// stream.read(&mut [0; 128])?;
|
2018-10-11 19:35:48 +00:00
|
|
|
/// Ok(())
|
2015-02-06 00:50:11 +00:00
|
|
|
/// } // the stream is closed here
|
|
|
|
/// ```
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-06 00:50:11 +00:00
|
|
|
pub struct TcpStream(net_imp::TcpStream);
|
|
|
|
|
2017-03-26 15:06:39 +00:00
|
|
|
/// A TCP socket server, listening for connections.
|
|
|
|
///
|
|
|
|
/// After creating a `TcpListener` by [`bind`]ing it to a socket address, it listens
|
|
|
|
/// for incoming TCP connections. These can be accepted by calling [`accept`] or by
|
2017-04-06 11:57:40 +00:00
|
|
|
/// iterating over the [`Incoming`] iterator returned by [`incoming`][`TcpListener::incoming`].
|
2017-03-26 15:06:39 +00:00
|
|
|
///
|
|
|
|
/// The socket will be closed when the value is dropped.
|
|
|
|
///
|
|
|
|
/// The Transmission Control Protocol is specified in [IETF RFC 793].
|
|
|
|
///
|
2020-08-15 17:17:19 +00:00
|
|
|
/// [`accept`]: TcpListener::accept
|
|
|
|
/// [`bind`]: TcpListener::bind
|
2017-03-26 15:06:39 +00:00
|
|
|
/// [IETF RFC 793]: https://tools.ietf.org/html/rfc793
|
2015-02-06 00:50:11 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// ```no_run
|
2015-02-06 00:50:11 +00:00
|
|
|
/// use std::net::{TcpListener, TcpStream};
|
|
|
|
///
|
|
|
|
/// fn handle_client(stream: TcpStream) {
|
|
|
|
/// // ...
|
|
|
|
/// }
|
|
|
|
///
|
2020-01-06 16:25:17 +00:00
|
|
|
/// fn main() -> std::io::Result<()> {
|
2018-07-17 12:18:58 +00:00
|
|
|
/// let listener = TcpListener::bind("127.0.0.1:80")?;
|
2017-04-25 08:10:06 +00:00
|
|
|
///
|
2018-03-25 02:56:07 +00:00
|
|
|
/// // accept connections and process them serially
|
|
|
|
/// for stream in listener.incoming() {
|
|
|
|
/// handle_client(stream?);
|
|
|
|
/// }
|
|
|
|
/// Ok(())
|
2015-02-06 00:50:11 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-06 00:50:11 +00:00
|
|
|
pub struct TcpListener(net_imp::TcpListener);
|
|
|
|
|
2017-03-26 12:30:03 +00:00
|
|
|
/// An iterator that infinitely [`accept`]s connections on a [`TcpListener`].
|
2016-08-02 00:16:56 +00:00
|
|
|
///
|
2020-08-15 17:17:19 +00:00
|
|
|
/// This `struct` is created by the [`TcpListener::incoming`] method.
|
2017-03-26 12:30:03 +00:00
|
|
|
/// See its documentation for more.
|
2016-08-02 00:16:56 +00:00
|
|
|
///
|
2020-08-15 17:17:19 +00:00
|
|
|
/// [`accept`]: TcpListener::accept
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-11-25 18:21:49 +00:00
|
|
|
#[derive(Debug)]
|
2019-11-27 18:29:00 +00:00
|
|
|
pub struct Incoming<'a> {
|
|
|
|
listener: &'a TcpListener,
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
|
|
|
|
impl TcpStream {
|
2015-04-13 14:21:32 +00:00
|
|
|
/// Opens a TCP connection to a remote host.
|
2015-02-06 00:50:11 +00:00
|
|
|
///
|
|
|
|
/// `addr` is an address of the remote host. Anything which implements
|
2017-03-25 16:11:08 +00:00
|
|
|
/// [`ToSocketAddrs`] trait can be supplied for the address; see this trait
|
2015-02-06 00:50:11 +00:00
|
|
|
/// documentation for concrete examples.
|
2017-08-31 02:11:48 +00:00
|
|
|
///
|
|
|
|
/// If `addr` yields multiple addresses, `connect` will be attempted with
|
|
|
|
/// each of the addresses until a connection is successful. If none of
|
|
|
|
/// the addresses result in a successful connection, the error returned from
|
|
|
|
/// the last connection attempt (the last address) is returned.
|
2016-11-25 17:54:42 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2017-08-31 02:11:48 +00:00
|
|
|
/// Open a TCP connection to `127.0.0.1:8080`:
|
|
|
|
///
|
2016-11-25 17:54:42 +00:00
|
|
|
/// ```no_run
|
|
|
|
/// use std::net::TcpStream;
|
|
|
|
///
|
|
|
|
/// if let Ok(stream) = TcpStream::connect("127.0.0.1:8080") {
|
|
|
|
/// println!("Connected to the server!");
|
|
|
|
/// } else {
|
|
|
|
/// println!("Couldn't connect to server...");
|
|
|
|
/// }
|
|
|
|
/// ```
|
2017-08-31 02:11:48 +00:00
|
|
|
///
|
|
|
|
/// Open a TCP connection to `127.0.0.1:8080`. If the connection fails, open
|
|
|
|
/// a TCP connection to `127.0.0.1:8081`:
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::net::{SocketAddr, TcpStream};
|
|
|
|
///
|
|
|
|
/// let addrs = [
|
|
|
|
/// SocketAddr::from(([127, 0, 0, 1], 8080)),
|
|
|
|
/// SocketAddr::from(([127, 0, 0, 1], 8081)),
|
|
|
|
/// ];
|
|
|
|
/// if let Ok(stream) = TcpStream::connect(&addrs[..]) {
|
|
|
|
/// println!("Connected to the server!");
|
|
|
|
/// } else {
|
|
|
|
/// println!("Couldn't connect to server...");
|
|
|
|
/// }
|
|
|
|
/// ```
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
pub fn connect<A: ToSocketAddrs>(addr: A) -> io::Result<TcpStream> {
|
2015-02-06 00:50:11 +00:00
|
|
|
super::each_addr(addr, net_imp::TcpStream::connect).map(TcpStream)
|
|
|
|
}
|
|
|
|
|
2017-07-05 06:46:24 +00:00
|
|
|
/// Opens a TCP connection to a remote host with a timeout.
|
|
|
|
///
|
|
|
|
/// Unlike `connect`, `connect_timeout` takes a single [`SocketAddr`] since
|
|
|
|
/// timeout must be applied to individual addresses.
|
|
|
|
///
|
|
|
|
/// It is an error to pass a zero `Duration` to this function.
|
|
|
|
///
|
|
|
|
/// Unlike other methods on `TcpStream`, this does not correspond to a
|
|
|
|
/// single system call. It instead calls `connect` in nonblocking mode and
|
|
|
|
/// then uses an OS-specific mechanism to await the completion of the
|
|
|
|
/// connection request.
|
2017-09-25 05:23:26 +00:00
|
|
|
#[stable(feature = "tcpstream_connect_timeout", since = "1.21.0")]
|
2017-07-05 06:46:24 +00:00
|
|
|
pub fn connect_timeout(addr: &SocketAddr, timeout: Duration) -> io::Result<TcpStream> {
|
|
|
|
net_imp::TcpStream::connect_timeout(addr, timeout).map(TcpStream)
|
|
|
|
}
|
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
/// Returns the socket address of the remote peer of this TCP connection.
|
2016-11-25 17:54:42 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpStream};
|
|
|
|
///
|
|
|
|
/// let stream = TcpStream::connect("127.0.0.1:8080")
|
|
|
|
/// .expect("Couldn't connect to the server...");
|
|
|
|
/// assert_eq!(stream.peer_addr().unwrap(),
|
|
|
|
/// SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080)));
|
|
|
|
/// ```
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-06 00:50:11 +00:00
|
|
|
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
|
|
|
|
self.0.peer_addr()
|
|
|
|
}
|
|
|
|
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
/// Returns the socket address of the local half of this TCP connection.
|
2016-11-25 17:54:42 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
2017-09-29 02:49:00 +00:00
|
|
|
/// use std::net::{IpAddr, Ipv4Addr, TcpStream};
|
2016-11-25 17:54:42 +00:00
|
|
|
///
|
|
|
|
/// let stream = TcpStream::connect("127.0.0.1:8080")
|
|
|
|
/// .expect("Couldn't connect to the server...");
|
2017-09-29 01:09:31 +00:00
|
|
|
/// assert_eq!(stream.local_addr().unwrap().ip(),
|
|
|
|
/// IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
|
2016-11-25 17:54:42 +00:00
|
|
|
/// ```
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
pub fn local_addr(&self) -> io::Result<SocketAddr> {
|
|
|
|
self.0.socket_addr()
|
|
|
|
}
|
|
|
|
|
2015-04-13 14:21:32 +00:00
|
|
|
/// Shuts down the read, write, or both halves of this connection.
|
2015-02-06 00:50:11 +00:00
|
|
|
///
|
|
|
|
/// This function will cause all pending and future I/O on the specified
|
|
|
|
/// portions to return immediately with an appropriate value (see the
|
2016-11-25 17:54:42 +00:00
|
|
|
/// documentation of [`Shutdown`]).
|
|
|
|
///
|
2017-04-24 11:20:16 +00:00
|
|
|
/// # Platform-specific behavior
|
|
|
|
///
|
|
|
|
/// Calling this function multiple times may result in different behavior,
|
|
|
|
/// depending on the operating system. On Linux, the second call will
|
|
|
|
/// return `Ok(())`, but on macOS, it will return `ErrorKind::NotConnected`.
|
|
|
|
/// This may change in the future.
|
|
|
|
///
|
2016-11-25 17:54:42 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::net::{Shutdown, TcpStream};
|
|
|
|
///
|
|
|
|
/// let stream = TcpStream::connect("127.0.0.1:8080")
|
|
|
|
/// .expect("Couldn't connect to the server...");
|
|
|
|
/// stream.shutdown(Shutdown::Both).expect("shutdown call failed");
|
|
|
|
/// ```
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-06 00:50:11 +00:00
|
|
|
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
|
|
|
|
self.0.shutdown(how)
|
|
|
|
}
|
|
|
|
|
2015-04-13 14:21:32 +00:00
|
|
|
/// Creates a new independently owned handle to the underlying socket.
|
2015-02-06 00:50:11 +00:00
|
|
|
///
|
|
|
|
/// The returned `TcpStream` is a reference to the same stream that this
|
|
|
|
/// object references. Both handles will read and write the same stream of
|
|
|
|
/// data, and options set on one stream will be propagated to the other
|
|
|
|
/// stream.
|
2016-11-25 17:54:42 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::net::TcpStream;
|
|
|
|
///
|
|
|
|
/// let stream = TcpStream::connect("127.0.0.1:8080")
|
|
|
|
/// .expect("Couldn't connect to the server...");
|
|
|
|
/// let stream_clone = stream.try_clone().expect("clone failed...");
|
|
|
|
/// ```
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-06 00:50:11 +00:00
|
|
|
pub fn try_clone(&self) -> io::Result<TcpStream> {
|
|
|
|
self.0.duplicate().map(TcpStream)
|
|
|
|
}
|
|
|
|
|
2015-05-27 06:47:03 +00:00
|
|
|
/// Sets the read timeout to the timeout specified.
|
|
|
|
///
|
2017-03-12 18:04:52 +00:00
|
|
|
/// If the value specified is [`None`], then [`read`] calls will block
|
2018-02-18 19:18:56 +00:00
|
|
|
/// indefinitely. An [`Err`] is returned if the zero [`Duration`] is
|
|
|
|
/// passed to this method.
|
2015-09-10 20:26:44 +00:00
|
|
|
///
|
2018-02-18 01:54:26 +00:00
|
|
|
/// # Platform-specific behavior
|
2015-09-10 20:26:44 +00:00
|
|
|
///
|
|
|
|
/// Platforms may return a different error code whenever a read times out as
|
|
|
|
/// a result of setting this option. For example Unix typically returns an
|
2016-11-25 17:54:42 +00:00
|
|
|
/// error of the kind [`WouldBlock`], but Windows may return [`TimedOut`].
|
|
|
|
///
|
2020-08-15 17:17:19 +00:00
|
|
|
/// [`read`]: Read::read
|
|
|
|
/// [`WouldBlock`]: io::ErrorKind::WouldBlock
|
|
|
|
/// [`TimedOut`]: io::ErrorKind::TimedOut
|
2016-11-25 17:54:42 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::net::TcpStream;
|
|
|
|
///
|
|
|
|
/// let stream = TcpStream::connect("127.0.0.1:8080")
|
|
|
|
/// .expect("Couldn't connect to the server...");
|
|
|
|
/// stream.set_read_timeout(None).expect("set_read_timeout call failed");
|
|
|
|
/// ```
|
2018-02-18 19:18:56 +00:00
|
|
|
///
|
|
|
|
/// An [`Err`] is returned if the zero [`Duration`] is passed to this
|
|
|
|
/// method:
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::io;
|
|
|
|
/// use std::net::TcpStream;
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// let stream = TcpStream::connect("127.0.0.1:8080").unwrap();
|
|
|
|
/// let result = stream.set_read_timeout(Some(Duration::new(0, 0)));
|
|
|
|
/// let err = result.unwrap_err();
|
|
|
|
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
|
|
|
|
/// ```
|
2015-09-10 20:26:44 +00:00
|
|
|
#[stable(feature = "socket_timeout", since = "1.4.0")]
|
2015-05-27 06:47:03 +00:00
|
|
|
pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
|
|
|
|
self.0.set_read_timeout(dur)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the write timeout to the timeout specified.
|
|
|
|
///
|
2017-03-12 18:04:52 +00:00
|
|
|
/// If the value specified is [`None`], then [`write`] calls will block
|
2018-02-18 19:18:56 +00:00
|
|
|
/// indefinitely. An [`Err`] is returned if the zero [`Duration`] is
|
|
|
|
/// passed to this method.
|
2015-09-10 20:26:44 +00:00
|
|
|
///
|
2018-02-18 01:54:26 +00:00
|
|
|
/// # Platform-specific behavior
|
2015-09-10 20:26:44 +00:00
|
|
|
///
|
|
|
|
/// Platforms may return a different error code whenever a write times out
|
|
|
|
/// as a result of setting this option. For example Unix typically returns
|
2016-11-25 17:54:42 +00:00
|
|
|
/// an error of the kind [`WouldBlock`], but Windows may return [`TimedOut`].
|
|
|
|
///
|
2020-08-15 17:17:19 +00:00
|
|
|
/// [`write`]: Write::write
|
|
|
|
/// [`WouldBlock`]: io::ErrorKind::WouldBlock
|
|
|
|
/// [`TimedOut`]: io::ErrorKind::TimedOut
|
2016-11-25 17:54:42 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::net::TcpStream;
|
|
|
|
///
|
|
|
|
/// let stream = TcpStream::connect("127.0.0.1:8080")
|
|
|
|
/// .expect("Couldn't connect to the server...");
|
|
|
|
/// stream.set_write_timeout(None).expect("set_write_timeout call failed");
|
|
|
|
/// ```
|
2018-02-18 19:18:56 +00:00
|
|
|
///
|
|
|
|
/// An [`Err`] is returned if the zero [`Duration`] is passed to this
|
|
|
|
/// method:
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::io;
|
|
|
|
/// use std::net::TcpStream;
|
|
|
|
/// use std::time::Duration;
|
|
|
|
///
|
|
|
|
/// let stream = TcpStream::connect("127.0.0.1:8080").unwrap();
|
|
|
|
/// let result = stream.set_write_timeout(Some(Duration::new(0, 0)));
|
|
|
|
/// let err = result.unwrap_err();
|
|
|
|
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
|
|
|
|
/// ```
|
2015-09-10 20:26:44 +00:00
|
|
|
#[stable(feature = "socket_timeout", since = "1.4.0")]
|
2015-05-27 06:47:03 +00:00
|
|
|
pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
|
|
|
|
self.0.set_write_timeout(dur)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the read timeout of this socket.
|
|
|
|
///
|
2017-03-12 18:04:52 +00:00
|
|
|
/// If the timeout is [`None`], then [`read`] calls will block indefinitely.
|
2015-05-27 06:47:03 +00:00
|
|
|
///
|
2018-02-18 01:54:26 +00:00
|
|
|
/// # Platform-specific behavior
|
2015-05-27 06:47:03 +00:00
|
|
|
///
|
|
|
|
/// Some platforms do not provide access to the current timeout.
|
2016-11-25 17:54:42 +00:00
|
|
|
///
|
2020-08-15 17:17:19 +00:00
|
|
|
/// [`read`]: Read::read
|
2016-11-25 17:54:42 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::net::TcpStream;
|
|
|
|
///
|
|
|
|
/// let stream = TcpStream::connect("127.0.0.1:8080")
|
|
|
|
/// .expect("Couldn't connect to the server...");
|
|
|
|
/// stream.set_read_timeout(None).expect("set_read_timeout call failed");
|
|
|
|
/// assert_eq!(stream.read_timeout().unwrap(), None);
|
|
|
|
/// ```
|
2015-09-10 20:26:44 +00:00
|
|
|
#[stable(feature = "socket_timeout", since = "1.4.0")]
|
2015-05-27 06:47:03 +00:00
|
|
|
pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
|
|
|
|
self.0.read_timeout()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the write timeout of this socket.
|
|
|
|
///
|
2017-03-12 18:04:52 +00:00
|
|
|
/// If the timeout is [`None`], then [`write`] calls will block indefinitely.
|
2015-05-27 06:47:03 +00:00
|
|
|
///
|
2018-02-18 01:54:26 +00:00
|
|
|
/// # Platform-specific behavior
|
2015-05-27 06:47:03 +00:00
|
|
|
///
|
|
|
|
/// Some platforms do not provide access to the current timeout.
|
2016-11-25 17:54:42 +00:00
|
|
|
///
|
2020-08-15 17:17:19 +00:00
|
|
|
/// [`write`]: Write::write
|
2016-11-25 17:54:42 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::net::TcpStream;
|
|
|
|
///
|
|
|
|
/// let stream = TcpStream::connect("127.0.0.1:8080")
|
|
|
|
/// .expect("Couldn't connect to the server...");
|
|
|
|
/// stream.set_write_timeout(None).expect("set_write_timeout call failed");
|
|
|
|
/// assert_eq!(stream.write_timeout().unwrap(), None);
|
|
|
|
/// ```
|
2015-09-10 20:26:44 +00:00
|
|
|
#[stable(feature = "socket_timeout", since = "1.4.0")]
|
2015-05-27 06:47:03 +00:00
|
|
|
pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
|
|
|
|
self.0.write_timeout()
|
|
|
|
}
|
2016-02-27 22:15:19 +00:00
|
|
|
|
2017-08-11 18:34:14 +00:00
|
|
|
/// Receives data on the socket from the remote address to which it is
|
2017-01-11 03:11:56 +00:00
|
|
|
/// connected, without removing that data from the queue. On success,
|
|
|
|
/// returns the number of bytes peeked.
|
|
|
|
///
|
|
|
|
/// Successive calls return the same data. This is accomplished by passing
|
|
|
|
/// `MSG_PEEK` as a flag to the underlying `recv` system call.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::net::TcpStream;
|
|
|
|
///
|
|
|
|
/// let stream = TcpStream::connect("127.0.0.1:8000")
|
|
|
|
/// .expect("couldn't bind to address");
|
|
|
|
/// let mut buf = [0; 10];
|
|
|
|
/// let len = stream.peek(&mut buf).expect("peek failed");
|
|
|
|
/// ```
|
2017-05-11 04:17:24 +00:00
|
|
|
#[stable(feature = "peek", since = "1.18.0")]
|
2017-01-11 03:11:56 +00:00
|
|
|
pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
self.0.peek(buf)
|
|
|
|
}
|
|
|
|
|
2016-02-27 22:15:19 +00:00
|
|
|
/// Sets the value of the `TCP_NODELAY` option on this socket.
|
|
|
|
///
|
|
|
|
/// If set, this option disables the Nagle algorithm. This means that
|
|
|
|
/// segments are always sent as soon as possible, even if there is only a
|
|
|
|
/// small amount of data. When not set, data is buffered until there is a
|
|
|
|
/// sufficient amount to send out, thereby avoiding the frequent sending of
|
|
|
|
/// small packets.
|
2016-11-25 17:54:42 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::net::TcpStream;
|
|
|
|
///
|
|
|
|
/// let stream = TcpStream::connect("127.0.0.1:8080")
|
|
|
|
/// .expect("Couldn't connect to the server...");
|
|
|
|
/// stream.set_nodelay(true).expect("set_nodelay call failed");
|
|
|
|
/// ```
|
2016-02-27 22:15:19 +00:00
|
|
|
#[stable(feature = "net2_mutators", since = "1.9.0")]
|
|
|
|
pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
|
|
|
|
self.0.set_nodelay(nodelay)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the value of the `TCP_NODELAY` option on this socket.
|
|
|
|
///
|
2020-08-15 17:17:19 +00:00
|
|
|
/// For more information about this option, see [`TcpStream::set_nodelay`].
|
2016-11-25 17:54:42 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::net::TcpStream;
|
|
|
|
///
|
|
|
|
/// let stream = TcpStream::connect("127.0.0.1:8080")
|
|
|
|
/// .expect("Couldn't connect to the server...");
|
|
|
|
/// stream.set_nodelay(true).expect("set_nodelay call failed");
|
|
|
|
/// assert_eq!(stream.nodelay().unwrap_or(false), true);
|
|
|
|
/// ```
|
2016-02-27 22:15:19 +00:00
|
|
|
#[stable(feature = "net2_mutators", since = "1.9.0")]
|
|
|
|
pub fn nodelay(&self) -> io::Result<bool> {
|
|
|
|
self.0.nodelay()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the value for the `IP_TTL` option on this socket.
|
|
|
|
///
|
|
|
|
/// This value sets the time-to-live field that is used in every packet sent
|
|
|
|
/// from this socket.
|
2016-11-25 17:54:42 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::net::TcpStream;
|
|
|
|
///
|
|
|
|
/// let stream = TcpStream::connect("127.0.0.1:8080")
|
|
|
|
/// .expect("Couldn't connect to the server...");
|
|
|
|
/// stream.set_ttl(100).expect("set_ttl call failed");
|
|
|
|
/// ```
|
2016-02-27 22:15:19 +00:00
|
|
|
#[stable(feature = "net2_mutators", since = "1.9.0")]
|
|
|
|
pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
|
|
|
|
self.0.set_ttl(ttl)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the value of the `IP_TTL` option for this socket.
|
|
|
|
///
|
2020-08-15 17:17:19 +00:00
|
|
|
/// For more information about this option, see [`TcpStream::set_ttl`].
|
2016-11-25 17:54:42 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::net::TcpStream;
|
|
|
|
///
|
|
|
|
/// let stream = TcpStream::connect("127.0.0.1:8080")
|
|
|
|
/// .expect("Couldn't connect to the server...");
|
|
|
|
/// stream.set_ttl(100).expect("set_ttl call failed");
|
|
|
|
/// assert_eq!(stream.ttl().unwrap_or(0), 100);
|
|
|
|
/// ```
|
2016-02-27 22:15:19 +00:00
|
|
|
#[stable(feature = "net2_mutators", since = "1.9.0")]
|
|
|
|
pub fn ttl(&self) -> io::Result<u32> {
|
|
|
|
self.0.ttl()
|
|
|
|
}
|
|
|
|
|
2019-02-09 22:16:58 +00:00
|
|
|
/// Gets the value of the `SO_ERROR` option on this socket.
|
2016-02-27 22:15:19 +00:00
|
|
|
///
|
|
|
|
/// This will retrieve the stored error in the underlying socket, clearing
|
|
|
|
/// the field in the process. This can be useful for checking errors between
|
|
|
|
/// calls.
|
2016-11-25 17:54:42 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::net::TcpStream;
|
|
|
|
///
|
|
|
|
/// let stream = TcpStream::connect("127.0.0.1:8080")
|
|
|
|
/// .expect("Couldn't connect to the server...");
|
|
|
|
/// stream.take_error().expect("No error was expected...");
|
|
|
|
/// ```
|
2016-02-27 22:15:19 +00:00
|
|
|
#[stable(feature = "net2_mutators", since = "1.9.0")]
|
|
|
|
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
|
|
|
|
self.0.take_error()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Moves this TCP stream into or out of nonblocking mode.
|
|
|
|
///
|
2017-10-12 01:21:49 +00:00
|
|
|
/// This will result in `read`, `write`, `recv` and `send` operations
|
2018-11-27 02:59:49 +00:00
|
|
|
/// becoming nonblocking, i.e., immediately returning from their calls.
|
2017-10-12 01:21:49 +00:00
|
|
|
/// If the IO operation is successful, `Ok` is returned and no further
|
|
|
|
/// action is required. If the IO operation could not be completed and needs
|
|
|
|
/// to be retried, an error with kind [`io::ErrorKind::WouldBlock`] is
|
|
|
|
/// returned.
|
|
|
|
///
|
|
|
|
/// On Unix platforms, calling this method corresponds to calling `fcntl`
|
|
|
|
/// `FIONBIO`. On Windows calling this method corresponds to calling
|
|
|
|
/// `ioctlsocket` `FIONBIO`.
|
2016-11-25 17:54:42 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2017-10-12 01:21:49 +00:00
|
|
|
/// Reading bytes from a TCP stream in non-blocking mode:
|
|
|
|
///
|
2016-11-25 17:54:42 +00:00
|
|
|
/// ```no_run
|
2017-10-12 01:21:49 +00:00
|
|
|
/// use std::io::{self, Read};
|
2016-11-25 17:54:42 +00:00
|
|
|
/// use std::net::TcpStream;
|
|
|
|
///
|
2017-10-12 01:21:49 +00:00
|
|
|
/// let mut stream = TcpStream::connect("127.0.0.1:7878")
|
|
|
|
/// .expect("Couldn't connect to the server...");
|
2016-11-25 17:54:42 +00:00
|
|
|
/// stream.set_nonblocking(true).expect("set_nonblocking call failed");
|
2017-10-12 01:21:49 +00:00
|
|
|
///
|
|
|
|
/// # fn wait_for_fd() { unimplemented!() }
|
|
|
|
/// let mut buf = vec![];
|
|
|
|
/// loop {
|
|
|
|
/// match stream.read_to_end(&mut buf) {
|
|
|
|
/// Ok(_) => break,
|
|
|
|
/// Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
|
|
|
|
/// // wait until network socket is ready, typically implemented
|
|
|
|
/// // via platform-specific APIs such as epoll or IOCP
|
|
|
|
/// wait_for_fd();
|
|
|
|
/// }
|
|
|
|
/// Err(e) => panic!("encountered IO error: {}", e),
|
|
|
|
/// };
|
|
|
|
/// };
|
|
|
|
/// println!("bytes: {:?}", buf);
|
2016-11-25 17:54:42 +00:00
|
|
|
/// ```
|
2016-02-27 22:15:19 +00:00
|
|
|
#[stable(feature = "net2_mutators", since = "1.9.0")]
|
|
|
|
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
|
|
|
|
self.0.set_nonblocking(nonblocking)
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-06 00:50:11 +00:00
|
|
|
impl Read for TcpStream {
|
2019-11-27 18:29:00 +00:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
self.0.read(buf)
|
|
|
|
}
|
2017-05-15 01:29:18 +00:00
|
|
|
|
2019-04-27 15:34:08 +00:00
|
|
|
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
|
2019-02-08 19:42:34 +00:00
|
|
|
self.0.read_vectored(bufs)
|
|
|
|
}
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
fn is_read_vectored(&self) -> bool {
|
|
|
|
self.0.is_read_vectored()
|
2020-01-03 19:26:05 +00:00
|
|
|
}
|
|
|
|
|
2017-05-15 01:29:18 +00:00
|
|
|
#[inline]
|
|
|
|
unsafe fn initializer(&self) -> Initializer {
|
2020-07-02 06:03:37 +00:00
|
|
|
// SAFETY: Read is guaranteed to work on uninitialized memory
|
|
|
|
unsafe { Initializer::nop() }
|
2015-07-10 16:34:07 +00:00
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-06 00:50:11 +00:00
|
|
|
impl Write for TcpStream {
|
2019-11-27 18:29:00 +00:00
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
self.0.write(buf)
|
|
|
|
}
|
2019-02-08 19:42:34 +00:00
|
|
|
|
2019-04-27 15:34:08 +00:00
|
|
|
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
2019-02-08 19:42:34 +00:00
|
|
|
self.0.write_vectored(bufs)
|
|
|
|
}
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
fn is_write_vectored(&self) -> bool {
|
|
|
|
self.0.is_write_vectored()
|
2020-01-03 19:26:05 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-02-18 03:42:36 +00:00
|
|
|
impl Read for &TcpStream {
|
2019-11-27 18:29:00 +00:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
self.0.read(buf)
|
|
|
|
}
|
2017-05-15 01:29:18 +00:00
|
|
|
|
2019-04-27 15:34:08 +00:00
|
|
|
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
|
2019-02-08 19:42:34 +00:00
|
|
|
self.0.read_vectored(bufs)
|
|
|
|
}
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
fn is_read_vectored(&self) -> bool {
|
|
|
|
self.0.is_read_vectored()
|
2020-01-03 19:26:05 +00:00
|
|
|
}
|
|
|
|
|
2017-05-15 01:29:18 +00:00
|
|
|
#[inline]
|
|
|
|
unsafe fn initializer(&self) -> Initializer {
|
2020-07-02 06:03:37 +00:00
|
|
|
// SAFETY: Read is guaranteed to work on uninitialized memory
|
|
|
|
unsafe { Initializer::nop() }
|
2015-07-10 16:34:07 +00:00
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-02-18 03:42:36 +00:00
|
|
|
impl Write for &TcpStream {
|
2019-11-27 18:29:00 +00:00
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
self.0.write(buf)
|
|
|
|
}
|
2019-02-08 19:42:34 +00:00
|
|
|
|
2019-04-27 15:34:08 +00:00
|
|
|
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
2019-02-08 19:42:34 +00:00
|
|
|
self.0.write_vectored(bufs)
|
|
|
|
}
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
fn is_write_vectored(&self) -> bool {
|
|
|
|
self.0.is_write_vectored()
|
2020-01-03 19:26:05 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AsInner<net_imp::TcpStream> for TcpStream {
|
2019-11-27 18:29:00 +00:00
|
|
|
fn as_inner(&self) -> &net_imp::TcpStream {
|
|
|
|
&self.0
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
std: Stabilize parts of std::os::platform::io
This commit stabilizes the platform-specific `io` modules, specifically around
the traits having to do with the raw representation of each object on each
platform.
Specifically, the following material was stabilized:
* `AsRaw{Fd,Socket,Handle}`
* `RawFd` (renamed from `Fd`)
* `RawHandle` (renamed from `Handle`)
* `RawSocket` (renamed from `Socket`)
* `AsRaw{Fd,Socket,Handle}` implementations
* `std::os::{unix, windows}::io`
The following material was added as `#[unstable]`:
* `FromRaw{Fd,Socket,Handle}`
* Implementations for various primitives
There are a number of future improvements that are possible to make to this
module, but this should cover a good bit of functionality desired from these
modules for now. Some specific future additions may include:
* `IntoRawXXX` traits to consume the raw representation and cancel the
auto-destructor.
* `Fd`, `Socket`, and `Handle` abstractions that behave like Rust objects and
have nice methods for various syscalls.
At this time though, these are considered backwards-compatible extensions and
will not be stabilized at this time.
This commit is a breaking change due to the addition of `Raw` in from of the
type aliases in each of the platform-specific modules.
[breaking-change]
2015-03-26 23:18:29 +00:00
|
|
|
impl FromInner<net_imp::TcpStream> for TcpStream {
|
2019-11-27 18:29:00 +00:00
|
|
|
fn from_inner(inner: net_imp::TcpStream) -> TcpStream {
|
|
|
|
TcpStream(inner)
|
|
|
|
}
|
std: Stabilize parts of std::os::platform::io
This commit stabilizes the platform-specific `io` modules, specifically around
the traits having to do with the raw representation of each object on each
platform.
Specifically, the following material was stabilized:
* `AsRaw{Fd,Socket,Handle}`
* `RawFd` (renamed from `Fd`)
* `RawHandle` (renamed from `Handle`)
* `RawSocket` (renamed from `Socket`)
* `AsRaw{Fd,Socket,Handle}` implementations
* `std::os::{unix, windows}::io`
The following material was added as `#[unstable]`:
* `FromRaw{Fd,Socket,Handle}`
* Implementations for various primitives
There are a number of future improvements that are possible to make to this
module, but this should cover a good bit of functionality desired from these
modules for now. Some specific future additions may include:
* `IntoRawXXX` traits to consume the raw representation and cancel the
auto-destructor.
* `Fd`, `Socket`, and `Handle` abstractions that behave like Rust objects and
have nice methods for various syscalls.
At this time though, these are considered backwards-compatible extensions and
will not be stabilized at this time.
This commit is a breaking change due to the addition of `Raw` in from of the
type aliases in each of the platform-specific modules.
[breaking-change]
2015-03-26 23:18:29 +00:00
|
|
|
}
|
|
|
|
|
2015-07-16 06:31:24 +00:00
|
|
|
impl IntoInner<net_imp::TcpStream> for TcpStream {
|
2019-11-27 18:29:00 +00:00
|
|
|
fn into_inner(self) -> net_imp::TcpStream {
|
|
|
|
self.0
|
|
|
|
}
|
2015-07-16 06:31:24 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 16:54:28 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-05-03 00:25:44 +00:00
|
|
|
impl fmt::Debug for TcpStream {
|
2019-03-01 08:34:11 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-05-03 00:25:44 +00:00
|
|
|
self.0.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
impl TcpListener {
|
|
|
|
/// Creates a new `TcpListener` which will be bound to the specified
|
|
|
|
/// address.
|
|
|
|
///
|
|
|
|
/// The returned listener is ready for accepting connections.
|
|
|
|
///
|
|
|
|
/// Binding with a port number of 0 will request that the OS assigns a port
|
|
|
|
/// to this listener. The port allocated can be queried via the
|
2020-08-15 17:17:19 +00:00
|
|
|
/// [`TcpListener::local_addr`] method.
|
2015-02-06 00:50:11 +00:00
|
|
|
///
|
2017-03-25 16:11:08 +00:00
|
|
|
/// The address type can be any implementor of [`ToSocketAddrs`] trait. See
|
2015-02-06 00:50:11 +00:00
|
|
|
/// its documentation for concrete examples.
|
2016-11-24 20:21:53 +00:00
|
|
|
///
|
2017-08-31 02:11:48 +00:00
|
|
|
/// If `addr` yields multiple addresses, `bind` will be attempted with
|
|
|
|
/// each of the addresses until one succeeds and returns the listener. If
|
|
|
|
/// none of the addresses succeed in creating a listener, the error returned
|
|
|
|
/// from the last attempt (the last address) is returned.
|
|
|
|
///
|
2016-11-24 20:21:53 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
2019-02-09 22:16:58 +00:00
|
|
|
/// Creates a TCP listener bound to `127.0.0.1:80`:
|
2017-08-31 02:11:48 +00:00
|
|
|
///
|
2016-11-24 20:21:53 +00:00
|
|
|
/// ```no_run
|
|
|
|
/// use std::net::TcpListener;
|
|
|
|
///
|
|
|
|
/// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
|
|
|
|
/// ```
|
2017-08-31 02:11:48 +00:00
|
|
|
///
|
2019-02-09 22:16:58 +00:00
|
|
|
/// Creates a TCP listener bound to `127.0.0.1:80`. If that fails, create a
|
2017-08-31 02:11:48 +00:00
|
|
|
/// TCP listener bound to `127.0.0.1:443`:
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::net::{SocketAddr, TcpListener};
|
|
|
|
///
|
|
|
|
/// let addrs = [
|
|
|
|
/// SocketAddr::from(([127, 0, 0, 1], 80)),
|
|
|
|
/// SocketAddr::from(([127, 0, 0, 1], 443)),
|
|
|
|
/// ];
|
|
|
|
/// let listener = TcpListener::bind(&addrs[..]).unwrap();
|
|
|
|
/// ```
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
pub fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<TcpListener> {
|
2015-02-06 00:50:11 +00:00
|
|
|
super::each_addr(addr, net_imp::TcpListener::bind).map(TcpListener)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the local socket address of this listener.
|
2016-11-24 20:21:53 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpListener};
|
|
|
|
///
|
|
|
|
/// let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
|
|
|
|
/// assert_eq!(listener.local_addr().unwrap(),
|
|
|
|
/// SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080)));
|
|
|
|
/// ```
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
pub fn local_addr(&self) -> io::Result<SocketAddr> {
|
|
|
|
self.0.socket_addr()
|
|
|
|
}
|
|
|
|
|
2015-04-13 14:21:32 +00:00
|
|
|
/// Creates a new independently owned handle to the underlying socket.
|
2015-02-06 00:50:11 +00:00
|
|
|
///
|
2017-03-25 16:11:08 +00:00
|
|
|
/// The returned [`TcpListener`] is a reference to the same socket that this
|
2015-02-06 00:50:11 +00:00
|
|
|
/// object references. Both handles can be used to accept incoming
|
|
|
|
/// connections and options set on one listener will affect the other.
|
2016-11-24 20:21:53 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::net::TcpListener;
|
|
|
|
///
|
|
|
|
/// let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
|
|
|
|
/// let listener_clone = listener.try_clone().unwrap();
|
|
|
|
/// ```
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-06 00:50:11 +00:00
|
|
|
pub fn try_clone(&self) -> io::Result<TcpListener> {
|
|
|
|
self.0.duplicate().map(TcpListener)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Accept a new incoming connection from this listener.
|
|
|
|
///
|
|
|
|
/// This function will block the calling thread until a new TCP connection
|
2017-03-25 16:11:08 +00:00
|
|
|
/// is established. When established, the corresponding [`TcpStream`] and the
|
2015-02-06 00:50:11 +00:00
|
|
|
/// remote peer's address will be returned.
|
2016-11-24 20:21:53 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::net::TcpListener;
|
|
|
|
///
|
|
|
|
/// let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
|
|
|
|
/// match listener.accept() {
|
|
|
|
/// Ok((_socket, addr)) => println!("new client: {:?}", addr),
|
|
|
|
/// Err(e) => println!("couldn't get client: {:?}", e),
|
|
|
|
/// }
|
|
|
|
/// ```
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-06 00:50:11 +00:00
|
|
|
pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
|
2018-10-24 16:33:29 +00:00
|
|
|
// On WASM, `TcpStream` is uninhabited (as it's unsupported) and so
|
|
|
|
// the `a` variable here is technically unused.
|
|
|
|
#[cfg_attr(target_arch = "wasm32", allow(unused_variables))]
|
2015-02-06 00:50:11 +00:00
|
|
|
self.0.accept().map(|(a, b)| (TcpStream(a), b))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator over the connections being received on this
|
|
|
|
/// listener.
|
|
|
|
///
|
2016-11-24 20:21:53 +00:00
|
|
|
/// The returned iterator will never return [`None`] and will also not yield
|
2017-03-26 12:30:03 +00:00
|
|
|
/// the peer's [`SocketAddr`] structure. Iterating over it is equivalent to
|
2020-08-15 17:17:19 +00:00
|
|
|
/// calling [`TcpListener::accept`] in a loop.
|
2016-11-24 20:21:53 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::net::TcpListener;
|
|
|
|
///
|
|
|
|
/// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
|
|
|
|
///
|
|
|
|
/// for stream in listener.incoming() {
|
|
|
|
/// match stream {
|
|
|
|
/// Ok(stream) => {
|
|
|
|
/// println!("new client!");
|
|
|
|
/// }
|
|
|
|
/// Err(e) => { /* connection failed */ }
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-03-01 08:34:11 +00:00
|
|
|
pub fn incoming(&self) -> Incoming<'_> {
|
2015-02-06 00:50:11 +00:00
|
|
|
Incoming { listener: self }
|
|
|
|
}
|
2016-02-27 22:15:19 +00:00
|
|
|
|
|
|
|
/// Sets the value for the `IP_TTL` option on this socket.
|
|
|
|
///
|
|
|
|
/// This value sets the time-to-live field that is used in every packet sent
|
|
|
|
/// from this socket.
|
2016-11-24 20:21:53 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::net::TcpListener;
|
|
|
|
///
|
|
|
|
/// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
|
|
|
|
/// listener.set_ttl(100).expect("could not set TTL");
|
|
|
|
/// ```
|
2016-02-27 22:15:19 +00:00
|
|
|
#[stable(feature = "net2_mutators", since = "1.9.0")]
|
|
|
|
pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
|
|
|
|
self.0.set_ttl(ttl)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the value of the `IP_TTL` option for this socket.
|
|
|
|
///
|
2020-08-15 17:17:19 +00:00
|
|
|
/// For more information about this option, see [`TcpListener::set_ttl`].
|
2016-11-24 20:21:53 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::net::TcpListener;
|
|
|
|
///
|
|
|
|
/// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
|
|
|
|
/// listener.set_ttl(100).expect("could not set TTL");
|
|
|
|
/// assert_eq!(listener.ttl().unwrap_or(0), 100);
|
|
|
|
/// ```
|
2016-02-27 22:15:19 +00:00
|
|
|
#[stable(feature = "net2_mutators", since = "1.9.0")]
|
|
|
|
pub fn ttl(&self) -> io::Result<u32> {
|
|
|
|
self.0.ttl()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "net2_mutators", since = "1.9.0")]
|
2019-11-27 18:29:00 +00:00
|
|
|
#[rustc_deprecated(
|
|
|
|
since = "1.16.0",
|
|
|
|
reason = "this option can only be set before the socket is bound"
|
|
|
|
)]
|
2016-12-11 17:52:36 +00:00
|
|
|
#[allow(missing_docs)]
|
2016-02-27 22:15:19 +00:00
|
|
|
pub fn set_only_v6(&self, only_v6: bool) -> io::Result<()> {
|
|
|
|
self.0.set_only_v6(only_v6)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "net2_mutators", since = "1.9.0")]
|
2019-11-27 18:29:00 +00:00
|
|
|
#[rustc_deprecated(
|
|
|
|
since = "1.16.0",
|
|
|
|
reason = "this option can only be set before the socket is bound"
|
|
|
|
)]
|
2016-12-11 17:52:36 +00:00
|
|
|
#[allow(missing_docs)]
|
2016-02-27 22:15:19 +00:00
|
|
|
pub fn only_v6(&self) -> io::Result<bool> {
|
|
|
|
self.0.only_v6()
|
|
|
|
}
|
|
|
|
|
2019-02-09 22:16:58 +00:00
|
|
|
/// Gets the value of the `SO_ERROR` option on this socket.
|
2016-02-27 22:15:19 +00:00
|
|
|
///
|
|
|
|
/// This will retrieve the stored error in the underlying socket, clearing
|
|
|
|
/// the field in the process. This can be useful for checking errors between
|
|
|
|
/// calls.
|
2016-11-24 20:21:53 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::net::TcpListener;
|
|
|
|
///
|
|
|
|
/// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
|
|
|
|
/// listener.take_error().expect("No error was expected");
|
|
|
|
/// ```
|
2016-02-27 22:15:19 +00:00
|
|
|
#[stable(feature = "net2_mutators", since = "1.9.0")]
|
|
|
|
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
|
|
|
|
self.0.take_error()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Moves this TCP stream into or out of nonblocking mode.
|
|
|
|
///
|
2017-10-12 01:21:49 +00:00
|
|
|
/// This will result in the `accept` operation becoming nonblocking,
|
2018-11-27 02:59:49 +00:00
|
|
|
/// i.e., immediately returning from their calls. If the IO operation is
|
2017-10-12 01:21:49 +00:00
|
|
|
/// successful, `Ok` is returned and no further action is required. If the
|
|
|
|
/// IO operation could not be completed and needs to be retried, an error
|
|
|
|
/// with kind [`io::ErrorKind::WouldBlock`] is returned.
|
|
|
|
///
|
|
|
|
/// On Unix platforms, calling this method corresponds to calling `fcntl`
|
|
|
|
/// `FIONBIO`. On Windows calling this method corresponds to calling
|
|
|
|
/// `ioctlsocket` `FIONBIO`.
|
2016-11-24 20:21:53 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2017-10-12 01:21:49 +00:00
|
|
|
/// Bind a TCP listener to an address, listen for connections, and read
|
|
|
|
/// bytes in nonblocking mode:
|
|
|
|
///
|
2016-11-24 20:21:53 +00:00
|
|
|
/// ```no_run
|
2017-10-12 01:21:49 +00:00
|
|
|
/// use std::io;
|
2016-11-24 20:21:53 +00:00
|
|
|
/// use std::net::TcpListener;
|
|
|
|
///
|
2017-10-12 01:21:49 +00:00
|
|
|
/// let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
|
2016-11-24 20:21:53 +00:00
|
|
|
/// listener.set_nonblocking(true).expect("Cannot set non-blocking");
|
2017-10-12 01:21:49 +00:00
|
|
|
///
|
|
|
|
/// # fn wait_for_fd() { unimplemented!() }
|
|
|
|
/// # fn handle_connection(stream: std::net::TcpStream) { unimplemented!() }
|
|
|
|
/// for stream in listener.incoming() {
|
|
|
|
/// match stream {
|
|
|
|
/// Ok(s) => {
|
|
|
|
/// // do something with the TcpStream
|
|
|
|
/// handle_connection(s);
|
|
|
|
/// }
|
|
|
|
/// Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
|
|
|
|
/// // wait until network socket is ready, typically implemented
|
|
|
|
/// // via platform-specific APIs such as epoll or IOCP
|
|
|
|
/// wait_for_fd();
|
|
|
|
/// continue;
|
|
|
|
/// }
|
|
|
|
/// Err(e) => panic!("encountered IO error: {}", e),
|
|
|
|
/// }
|
|
|
|
/// }
|
2016-11-24 20:21:53 +00:00
|
|
|
/// ```
|
2016-02-27 22:15:19 +00:00
|
|
|
#[stable(feature = "net2_mutators", since = "1.9.0")]
|
|
|
|
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
|
|
|
|
self.0.set_nonblocking(nonblocking)
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-06 00:50:11 +00:00
|
|
|
impl<'a> Iterator for Incoming<'a> {
|
|
|
|
type Item = io::Result<TcpStream>;
|
|
|
|
fn next(&mut self) -> Option<io::Result<TcpStream>> {
|
|
|
|
Some(self.listener.accept().map(|p| p.0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsInner<net_imp::TcpListener> for TcpListener {
|
2019-11-27 18:29:00 +00:00
|
|
|
fn as_inner(&self) -> &net_imp::TcpListener {
|
|
|
|
&self.0
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
std: Stabilize parts of std::os::platform::io
This commit stabilizes the platform-specific `io` modules, specifically around
the traits having to do with the raw representation of each object on each
platform.
Specifically, the following material was stabilized:
* `AsRaw{Fd,Socket,Handle}`
* `RawFd` (renamed from `Fd`)
* `RawHandle` (renamed from `Handle`)
* `RawSocket` (renamed from `Socket`)
* `AsRaw{Fd,Socket,Handle}` implementations
* `std::os::{unix, windows}::io`
The following material was added as `#[unstable]`:
* `FromRaw{Fd,Socket,Handle}`
* Implementations for various primitives
There are a number of future improvements that are possible to make to this
module, but this should cover a good bit of functionality desired from these
modules for now. Some specific future additions may include:
* `IntoRawXXX` traits to consume the raw representation and cancel the
auto-destructor.
* `Fd`, `Socket`, and `Handle` abstractions that behave like Rust objects and
have nice methods for various syscalls.
At this time though, these are considered backwards-compatible extensions and
will not be stabilized at this time.
This commit is a breaking change due to the addition of `Raw` in from of the
type aliases in each of the platform-specific modules.
[breaking-change]
2015-03-26 23:18:29 +00:00
|
|
|
impl FromInner<net_imp::TcpListener> for TcpListener {
|
|
|
|
fn from_inner(inner: net_imp::TcpListener) -> TcpListener {
|
|
|
|
TcpListener(inner)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-16 06:31:24 +00:00
|
|
|
impl IntoInner<net_imp::TcpListener> for TcpListener {
|
2019-11-27 18:29:00 +00:00
|
|
|
fn into_inner(self) -> net_imp::TcpListener {
|
|
|
|
self.0
|
|
|
|
}
|
2015-07-16 06:31:24 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 16:54:28 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-05-03 00:25:44 +00:00
|
|
|
impl fmt::Debug for TcpListener {
|
2019-03-01 08:34:11 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-05-03 00:25:44 +00:00
|
|
|
self.0.fmt(f)
|
|
|
|
}
|
|
|
|
}
|