2015-02-06 00:50:11 +00:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use io::prelude::*;
|
|
|
|
|
2015-05-03 00:25:44 +00:00
|
|
|
use fmt;
|
2017-05-15 01:29:18 +00:00
|
|
|
use io::{self, Initializer};
|
2015-02-06 00:50:11 +00:00
|
|
|
use net::{ToSocketAddrs, SocketAddr, Shutdown};
|
2015-07-16 06:31:24 +00:00
|
|
|
use sys_common::net as net_imp;
|
|
|
|
use sys_common::{AsInner, FromInner, IntoInner};
|
2015-05-27 06:47:03 +00:00
|
|
|
use 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].
|
|
|
|
///
|
|
|
|
/// [`accept`]: ../../std/net/struct.TcpListener.html#method.accept
|
|
|
|
/// [`connect`]: #method.connect
|
|
|
|
/// [IETF RFC 793]: https://tools.ietf.org/html/rfc793
|
|
|
|
/// [reading]: ../../std/io/trait.Read.html
|
|
|
|
/// [`shutdown`]: #method.shutdown
|
|
|
|
/// [`TcpListener`]: ../../std/net/struct.TcpListener.html
|
|
|
|
/// [writing]: ../../std/io/trait.Write.html
|
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;
|
|
|
|
///
|
|
|
|
/// {
|
|
|
|
/// let mut stream = TcpStream::connect("127.0.0.1:34254").unwrap();
|
|
|
|
///
|
|
|
|
/// // ignore the Result
|
|
|
|
/// let _ = stream.write(&[1]);
|
|
|
|
/// let _ = stream.read(&mut [0; 128]); // ignore here too
|
|
|
|
/// } // 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].
|
|
|
|
///
|
|
|
|
/// [`accept`]: #method.accept
|
|
|
|
/// [`bind`]: #method.bind
|
|
|
|
/// [IETF RFC 793]: https://tools.ietf.org/html/rfc793
|
|
|
|
/// [`Incoming`]: ../../std/net/struct.Incoming.html
|
2017-04-06 11:57:40 +00:00
|
|
|
/// [`TcpListener::incoming`]: #method.incoming
|
2015-02-06 00:50:11 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2017-04-25 08:10:06 +00:00
|
|
|
/// ```
|
|
|
|
/// # use std::io;
|
2015-02-06 00:50:11 +00:00
|
|
|
/// use std::net::{TcpListener, TcpStream};
|
|
|
|
///
|
|
|
|
/// fn handle_client(stream: TcpStream) {
|
|
|
|
/// // ...
|
|
|
|
/// }
|
|
|
|
///
|
2017-04-25 08:10:06 +00:00
|
|
|
/// # fn process() -> io::Result<()> {
|
|
|
|
/// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
|
|
|
|
///
|
2017-01-03 19:52:14 +00:00
|
|
|
/// // accept connections and process them serially
|
2015-02-06 00:50:11 +00:00
|
|
|
/// for stream in listener.incoming() {
|
2017-04-25 08:10:06 +00:00
|
|
|
/// handle_client(stream?);
|
2015-02-06 00:50:11 +00:00
|
|
|
/// }
|
2017-04-25 08:10:06 +00:00
|
|
|
/// # 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
|
|
|
///
|
|
|
|
/// This `struct` is created by the [`incoming`] method on [`TcpListener`].
|
2017-03-26 12:30:03 +00:00
|
|
|
/// See its documentation for more.
|
2016-08-02 00:16:56 +00:00
|
|
|
///
|
2017-03-26 12:30:03 +00:00
|
|
|
/// [`accept`]: ../../std/net/struct.TcpListener.html#method.accept
|
|
|
|
/// [`incoming`]: ../../std/net/struct.TcpListener.html#method.incoming
|
|
|
|
/// [`TcpListener`]: ../../std/net/struct.TcpListener.html
|
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)]
|
2015-02-06 00:50:11 +00:00
|
|
|
pub struct Incoming<'a> { listener: &'a TcpListener }
|
|
|
|
|
|
|
|
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
|
|
|
///
|
2017-03-25 16:11:08 +00:00
|
|
|
/// [`ToSocketAddrs`]: ../../std/net/trait.ToSocketAddrs.html
|
|
|
|
///
|
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.
|
|
|
|
///
|
|
|
|
/// [`SocketAddr`]: ../../std/net/enum.SocketAddr.html
|
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`]).
|
|
|
|
///
|
|
|
|
/// [`Shutdown`]: ../../std/net/enum.Shutdown.html
|
|
|
|
///
|
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
|
2015-05-27 06:47:03 +00:00
|
|
|
/// indefinitely. It is an error to pass the zero `Duration` 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`].
|
|
|
|
///
|
|
|
|
/// [`None`]: ../../std/option/enum.Option.html#variant.None
|
2017-03-12 18:04:52 +00:00
|
|
|
/// [`read`]: ../../std/io/trait.Read.html#tymethod.read
|
2016-11-25 17:54:42 +00:00
|
|
|
/// [`WouldBlock`]: ../../std/io/enum.ErrorKind.html#variant.WouldBlock
|
|
|
|
/// [`TimedOut`]: ../../std/io/enum.ErrorKind.html#variant.TimedOut
|
|
|
|
///
|
|
|
|
/// # 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");
|
|
|
|
/// ```
|
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
|
2016-11-25 17:54:42 +00:00
|
|
|
/// indefinitely. It is an error to pass the zero [`Duration`] to this
|
2015-05-27 06:47:03 +00:00
|
|
|
/// 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`].
|
|
|
|
///
|
|
|
|
/// [`None`]: ../../std/option/enum.Option.html#variant.None
|
2017-03-12 18:04:52 +00:00
|
|
|
/// [`write`]: ../../std/io/trait.Write.html#tymethod.write
|
2016-11-25 17:54:42 +00:00
|
|
|
/// [`Duration`]: ../../std/time/struct.Duration.html
|
|
|
|
/// [`WouldBlock`]: ../../std/io/enum.ErrorKind.html#variant.WouldBlock
|
|
|
|
/// [`TimedOut`]: ../../std/io/enum.ErrorKind.html#variant.TimedOut
|
|
|
|
///
|
|
|
|
/// # 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");
|
|
|
|
/// ```
|
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
|
|
|
///
|
|
|
|
/// [`None`]: ../../std/option/enum.Option.html#variant.None
|
2017-03-12 18:04:52 +00:00
|
|
|
/// [`read`]: ../../std/io/trait.Read.html#tymethod.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
|
|
|
///
|
|
|
|
/// [`None`]: ../../std/option/enum.Option.html#variant.None
|
2017-03-12 18:04:52 +00:00
|
|
|
/// [`write`]: ../../std/io/trait.Write.html#tymethod.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.
|
|
|
|
///
|
|
|
|
/// For more information about this option, see [`set_nodelay`][link].
|
|
|
|
///
|
2016-03-26 10:59:30 +00:00
|
|
|
/// [link]: #method.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.
|
|
|
|
///
|
|
|
|
/// For more information about this option, see [`set_ttl`][link].
|
|
|
|
///
|
2016-03-26 10:59:30 +00:00
|
|
|
/// [link]: #method.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()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the value of the `SO_ERROR` option on this socket.
|
|
|
|
///
|
|
|
|
/// 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
|
|
|
|
/// becoming nonblocking, i.e. immediately returning from their calls.
|
|
|
|
/// 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
|
|
|
/// ```
|
2017-10-12 01:21:49 +00:00
|
|
|
///
|
|
|
|
/// [`io::ErrorKind::WouldBlock`]: ../io/enum.ErrorKind.html#variant.WouldBlock
|
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 {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { self.0.read(buf) }
|
2017-05-15 01:29:18 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
unsafe fn initializer(&self) -> Initializer {
|
|
|
|
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 {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { self.0.write(buf) }
|
|
|
|
fn flush(&mut self) -> io::Result<()> { Ok(()) }
|
|
|
|
}
|
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> Read for &'a TcpStream {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { self.0.read(buf) }
|
2017-05-15 01:29:18 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
unsafe fn initializer(&self) -> Initializer {
|
|
|
|
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<'a> Write for &'a TcpStream {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { self.0.write(buf) }
|
|
|
|
fn flush(&mut self) -> io::Result<()> { Ok(()) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsInner<net_imp::TcpStream> for TcpStream {
|
|
|
|
fn as_inner(&self) -> &net_imp::TcpStream { &self.0 }
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
fn from_inner(inner: net_imp::TcpStream) -> TcpStream { TcpStream(inner) }
|
|
|
|
}
|
|
|
|
|
2015-07-16 06:31:24 +00:00
|
|
|
impl IntoInner<net_imp::TcpStream> for TcpStream {
|
|
|
|
fn into_inner(self) -> net_imp::TcpStream { self.0 }
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
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
|
2017-03-25 16:11:08 +00:00
|
|
|
/// [`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.
|
|
|
|
///
|
2017-03-25 16:11:08 +00:00
|
|
|
/// [`local_addr`]: #method.local_addr
|
|
|
|
/// [`ToSocketAddrs`]: ../../std/net/trait.ToSocketAddrs.html
|
|
|
|
///
|
2016-11-24 20:21:53 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
2017-08-31 02:11:48 +00:00
|
|
|
/// Create a TCP listener bound to `127.0.0.1:80`:
|
|
|
|
///
|
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
|
|
|
///
|
|
|
|
/// Create a TCP listener bound to `127.0.0.1:80`. If that fails, create a
|
|
|
|
/// 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
|
|
|
///
|
2017-03-25 16:11:08 +00:00
|
|
|
/// [`TcpListener`]: ../../std/net/struct.TcpListener.html
|
|
|
|
///
|
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
|
|
|
///
|
2017-03-25 16:11:08 +00:00
|
|
|
/// [`TcpStream`]: ../../std/net/struct.TcpStream.html
|
|
|
|
///
|
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)> {
|
|
|
|
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
|
|
|
|
/// calling [`accept`] in a loop.
|
2016-11-24 20:21:53 +00:00
|
|
|
///
|
|
|
|
/// [`None`]: ../../std/option/enum.Option.html#variant.None
|
|
|
|
/// [`SocketAddr`]: ../../std/net/enum.SocketAddr.html
|
2017-03-26 12:30:03 +00:00
|
|
|
/// [`accept`]: #method.accept
|
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")]
|
2015-02-06 00:50:11 +00:00
|
|
|
pub fn incoming(&self) -> Incoming {
|
|
|
|
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.
|
|
|
|
///
|
2017-03-12 18:04:52 +00:00
|
|
|
/// For more information about this option, see [`set_ttl`][link].
|
2016-02-27 22:15:19 +00:00
|
|
|
///
|
2016-03-26 10:59:30 +00:00
|
|
|
/// [link]: #method.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")]
|
2016-12-11 17:52:36 +00:00
|
|
|
#[rustc_deprecated(since = "1.16.0",
|
|
|
|
reason = "this option can only be set before the socket is bound")]
|
|
|
|
#[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")]
|
2016-12-11 17:52:36 +00:00
|
|
|
#[rustc_deprecated(since = "1.16.0",
|
|
|
|
reason = "this option can only be set before the socket is bound")]
|
|
|
|
#[allow(missing_docs)]
|
2016-02-27 22:15:19 +00:00
|
|
|
pub fn only_v6(&self) -> io::Result<bool> {
|
|
|
|
self.0.only_v6()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the value of the `SO_ERROR` option on this socket.
|
|
|
|
///
|
|
|
|
/// 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,
|
|
|
|
/// i.e. immediately returning from their calls. 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-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
|
|
|
/// ```
|
2017-10-12 01:21:49 +00:00
|
|
|
///
|
|
|
|
/// [`io::ErrorKind::WouldBlock`]: ../io/enum.ErrorKind.html#variant.WouldBlock
|
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 {
|
|
|
|
fn as_inner(&self) -> &net_imp::TcpListener { &self.0 }
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
fn into_inner(self) -> net_imp::TcpListener { self.0 }
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self.0.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 10:20:50 +00:00
|
|
|
#[cfg(all(test, not(any(target_os = "cloudabi", target_os = "emscripten"))))]
|
2015-02-06 00:50:11 +00:00
|
|
|
mod tests {
|
|
|
|
use io::ErrorKind;
|
|
|
|
use io::prelude::*;
|
|
|
|
use net::*;
|
|
|
|
use net::test::{next_test_ip4, next_test_ip6};
|
|
|
|
use sync::mpsc::channel;
|
2015-05-04 01:01:25 +00:00
|
|
|
use sys_common::AsInner;
|
2015-12-18 12:29:49 +00:00
|
|
|
use time::{Instant, Duration};
|
2015-02-17 23:10:25 +00:00
|
|
|
use thread;
|
2015-02-06 00:50:11 +00:00
|
|
|
|
|
|
|
fn each_ip(f: &mut FnMut(SocketAddr)) {
|
|
|
|
f(next_test_ip4());
|
|
|
|
f(next_test_ip6());
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! t {
|
|
|
|
($e:expr) => {
|
|
|
|
match $e {
|
|
|
|
Ok(t) => t,
|
|
|
|
Err(e) => panic!("received error for `{}`: {}", stringify!($e), e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bind_error() {
|
2015-03-05 23:33:24 +00:00
|
|
|
match TcpListener::bind("1.1.1.1:9999") {
|
2015-02-06 00:50:11 +00:00
|
|
|
Ok(..) => panic!(),
|
2015-03-05 23:33:24 +00:00
|
|
|
Err(e) =>
|
2015-03-17 01:08:57 +00:00
|
|
|
assert_eq!(e.kind(), ErrorKind::AddrNotAvailable),
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn connect_error() {
|
|
|
|
match TcpStream::connect("0.0.0.0:1") {
|
|
|
|
Ok(..) => panic!(),
|
2015-03-17 01:08:57 +00:00
|
|
|
Err(e) => assert!(e.kind() == ErrorKind::ConnectionRefused ||
|
|
|
|
e.kind() == ErrorKind::InvalidInput ||
|
|
|
|
e.kind() == ErrorKind::AddrInUse ||
|
|
|
|
e.kind() == ErrorKind::AddrNotAvailable,
|
|
|
|
"bad error: {} {:?}", e, e.kind()),
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn listen_localhost() {
|
|
|
|
let socket_addr = next_test_ip4();
|
|
|
|
let listener = t!(TcpListener::bind(&socket_addr));
|
|
|
|
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move || {
|
2015-02-06 00:50:11 +00:00
|
|
|
let mut stream = t!(TcpStream::connect(&("localhost",
|
|
|
|
socket_addr.port())));
|
|
|
|
t!(stream.write(&[144]));
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut stream = t!(listener.accept()).0;
|
|
|
|
let mut buf = [0];
|
|
|
|
t!(stream.read(&mut buf));
|
|
|
|
assert!(buf[0] == 144);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-10-19 04:19:01 +00:00
|
|
|
fn connect_loopback() {
|
|
|
|
each_ip(&mut |addr| {
|
|
|
|
let acceptor = t!(TcpListener::bind(&addr));
|
2015-02-06 00:50:11 +00:00
|
|
|
|
2015-10-19 04:19:01 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
|
|
|
let host = match addr {
|
|
|
|
SocketAddr::V4(..) => "127.0.0.1",
|
|
|
|
SocketAddr::V6(..) => "::1",
|
|
|
|
};
|
|
|
|
let mut stream = t!(TcpStream::connect(&(host, addr.port())));
|
|
|
|
t!(stream.write(&[66]));
|
|
|
|
});
|
2015-02-06 00:50:11 +00:00
|
|
|
|
2015-10-19 04:19:01 +00:00
|
|
|
let mut stream = t!(acceptor.accept()).0;
|
|
|
|
let mut buf = [0];
|
|
|
|
t!(stream.read(&mut buf));
|
|
|
|
assert!(buf[0] == 66);
|
|
|
|
})
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-10-19 04:19:01 +00:00
|
|
|
fn smoke_test() {
|
2015-02-06 00:50:11 +00:00
|
|
|
each_ip(&mut |addr| {
|
|
|
|
let acceptor = t!(TcpListener::bind(&addr));
|
|
|
|
|
|
|
|
let (tx, rx) = channel();
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
let mut stream = t!(TcpStream::connect(&addr));
|
|
|
|
t!(stream.write(&[99]));
|
2015-03-30 18:00:05 +00:00
|
|
|
tx.send(t!(stream.local_addr())).unwrap();
|
2015-02-06 00:50:11 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let (mut stream, addr) = t!(acceptor.accept());
|
|
|
|
let mut buf = [0];
|
|
|
|
t!(stream.read(&mut buf));
|
|
|
|
assert!(buf[0] == 99);
|
|
|
|
assert_eq!(addr, t!(rx.recv()));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-10-19 04:19:01 +00:00
|
|
|
fn read_eof() {
|
2015-02-06 00:50:11 +00:00
|
|
|
each_ip(&mut |addr| {
|
|
|
|
let acceptor = t!(TcpListener::bind(&addr));
|
|
|
|
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
let _stream = t!(TcpStream::connect(&addr));
|
|
|
|
// Close
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut stream = t!(acceptor.accept()).0;
|
|
|
|
let mut buf = [0];
|
|
|
|
let nread = t!(stream.read(&mut buf));
|
|
|
|
assert_eq!(nread, 0);
|
|
|
|
let nread = t!(stream.read(&mut buf));
|
|
|
|
assert_eq!(nread, 0);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn write_close() {
|
|
|
|
each_ip(&mut |addr| {
|
|
|
|
let acceptor = t!(TcpListener::bind(&addr));
|
|
|
|
|
|
|
|
let (tx, rx) = channel();
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
drop(t!(TcpStream::connect(&addr)));
|
|
|
|
tx.send(()).unwrap();
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut stream = t!(acceptor.accept()).0;
|
|
|
|
rx.recv().unwrap();
|
|
|
|
let buf = [0];
|
|
|
|
match stream.write(&buf) {
|
|
|
|
Ok(..) => {}
|
|
|
|
Err(e) => {
|
|
|
|
assert!(e.kind() == ErrorKind::ConnectionReset ||
|
|
|
|
e.kind() == ErrorKind::BrokenPipe ||
|
|
|
|
e.kind() == ErrorKind::ConnectionAborted,
|
|
|
|
"unknown error: {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-10-19 04:19:01 +00:00
|
|
|
fn multiple_connect_serial() {
|
2015-02-06 00:50:11 +00:00
|
|
|
each_ip(&mut |addr| {
|
|
|
|
let max = 10;
|
|
|
|
let acceptor = t!(TcpListener::bind(&addr));
|
|
|
|
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
for _ in 0..max {
|
|
|
|
let mut stream = t!(TcpStream::connect(&addr));
|
|
|
|
t!(stream.write(&[99]));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
for stream in acceptor.incoming().take(max) {
|
|
|
|
let mut stream = t!(stream);
|
|
|
|
let mut buf = [0];
|
|
|
|
t!(stream.read(&mut buf));
|
|
|
|
assert_eq!(buf[0], 99);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_connect_interleaved_greedy_schedule() {
|
2015-06-07 17:50:13 +00:00
|
|
|
const MAX: usize = 10;
|
2015-02-06 00:50:11 +00:00
|
|
|
each_ip(&mut |addr| {
|
|
|
|
let acceptor = t!(TcpListener::bind(&addr));
|
|
|
|
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
let acceptor = acceptor;
|
|
|
|
for (i, stream) in acceptor.incoming().enumerate().take(MAX) {
|
2015-05-08 15:12:29 +00:00
|
|
|
// Start another thread to handle the connection
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
let mut stream = t!(stream);
|
|
|
|
let mut buf = [0];
|
|
|
|
t!(stream.read(&mut buf));
|
|
|
|
assert!(buf[0] == i as u8);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
connect(0, addr);
|
|
|
|
});
|
|
|
|
|
|
|
|
fn connect(i: usize, addr: SocketAddr) {
|
|
|
|
if i == MAX { return }
|
|
|
|
|
2015-02-17 23:10:25 +00:00
|
|
|
let t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
let mut stream = t!(TcpStream::connect(&addr));
|
|
|
|
// Connect again before writing
|
|
|
|
connect(i + 1, addr);
|
|
|
|
t!(stream.write(&[i as u8]));
|
|
|
|
});
|
|
|
|
t.join().ok().unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-10-19 04:19:01 +00:00
|
|
|
fn multiple_connect_interleaved_lazy_schedule() {
|
2015-02-27 14:36:53 +00:00
|
|
|
const MAX: usize = 10;
|
2015-02-06 00:50:11 +00:00
|
|
|
each_ip(&mut |addr| {
|
|
|
|
let acceptor = t!(TcpListener::bind(&addr));
|
|
|
|
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
for stream in acceptor.incoming().take(MAX) {
|
2015-05-08 15:12:29 +00:00
|
|
|
// Start another thread to handle the connection
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
let mut stream = t!(stream);
|
|
|
|
let mut buf = [0];
|
|
|
|
t!(stream.read(&mut buf));
|
|
|
|
assert!(buf[0] == 99);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
connect(0, addr);
|
|
|
|
});
|
|
|
|
|
|
|
|
fn connect(i: usize, addr: SocketAddr) {
|
|
|
|
if i == MAX { return }
|
|
|
|
|
2015-02-17 23:10:25 +00:00
|
|
|
let t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
let mut stream = t!(TcpStream::connect(&addr));
|
|
|
|
connect(i + 1, addr);
|
|
|
|
t!(stream.write(&[99]));
|
|
|
|
});
|
|
|
|
t.join().ok().unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-10-19 04:19:01 +00:00
|
|
|
fn socket_and_peer_name() {
|
2015-02-06 00:50:11 +00:00
|
|
|
each_ip(&mut |addr| {
|
|
|
|
let listener = t!(TcpListener::bind(&addr));
|
2015-03-30 18:00:05 +00:00
|
|
|
let so_name = t!(listener.local_addr());
|
2015-02-06 00:50:11 +00:00
|
|
|
assert_eq!(addr, so_name);
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
t!(listener.accept());
|
|
|
|
});
|
|
|
|
|
|
|
|
let stream = t!(TcpStream::connect(&addr));
|
|
|
|
assert_eq!(addr, t!(stream.peer_addr()));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn partial_read() {
|
|
|
|
each_ip(&mut |addr| {
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
let srv = t!(TcpListener::bind(&addr));
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
let mut cl = t!(srv.accept()).0;
|
|
|
|
cl.write(&[10]).unwrap();
|
|
|
|
let mut b = [0];
|
|
|
|
t!(cl.read(&mut b));
|
|
|
|
tx.send(()).unwrap();
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut c = t!(TcpStream::connect(&addr));
|
|
|
|
let mut b = [0; 10];
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(c.read(&mut b).unwrap(), 1);
|
2015-02-06 00:50:11 +00:00
|
|
|
t!(c.write(&[1]));
|
|
|
|
rx.recv().unwrap();
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn double_bind() {
|
|
|
|
each_ip(&mut |addr| {
|
|
|
|
let _listener = t!(TcpListener::bind(&addr));
|
|
|
|
match TcpListener::bind(&addr) {
|
|
|
|
Ok(..) => panic!(),
|
|
|
|
Err(e) => {
|
|
|
|
assert!(e.kind() == ErrorKind::ConnectionRefused ||
|
2015-03-17 01:08:57 +00:00
|
|
|
e.kind() == ErrorKind::Other ||
|
|
|
|
e.kind() == ErrorKind::AddrInUse,
|
2015-02-06 00:50:11 +00:00
|
|
|
"unknown error: {} {:?}", e, e.kind());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fast_rebind() {
|
|
|
|
each_ip(&mut |addr| {
|
|
|
|
let acceptor = t!(TcpListener::bind(&addr));
|
|
|
|
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
t!(TcpStream::connect(&addr));
|
|
|
|
});
|
|
|
|
|
|
|
|
t!(acceptor.accept());
|
|
|
|
drop(acceptor);
|
|
|
|
t!(TcpListener::bind(&addr));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn tcp_clone_smoke() {
|
|
|
|
each_ip(&mut |addr| {
|
|
|
|
let acceptor = t!(TcpListener::bind(&addr));
|
|
|
|
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
let mut s = t!(TcpStream::connect(&addr));
|
|
|
|
let mut buf = [0, 0];
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(s.read(&mut buf).unwrap(), 1);
|
2015-02-06 00:50:11 +00:00
|
|
|
assert_eq!(buf[0], 1);
|
|
|
|
t!(s.write(&[2]));
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut s1 = t!(acceptor.accept()).0;
|
|
|
|
let s2 = t!(s1.try_clone());
|
|
|
|
|
|
|
|
let (tx1, rx1) = channel();
|
|
|
|
let (tx2, rx2) = channel();
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
let mut s2 = s2;
|
|
|
|
rx1.recv().unwrap();
|
|
|
|
t!(s2.write(&[1]));
|
|
|
|
tx2.send(()).unwrap();
|
|
|
|
});
|
|
|
|
tx1.send(()).unwrap();
|
|
|
|
let mut buf = [0, 0];
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(s1.read(&mut buf).unwrap(), 1);
|
2015-02-06 00:50:11 +00:00
|
|
|
rx2.recv().unwrap();
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn tcp_clone_two_read() {
|
|
|
|
each_ip(&mut |addr| {
|
|
|
|
let acceptor = t!(TcpListener::bind(&addr));
|
|
|
|
let (tx1, rx) = channel();
|
|
|
|
let tx2 = tx1.clone();
|
|
|
|
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
let mut s = t!(TcpStream::connect(&addr));
|
|
|
|
t!(s.write(&[1]));
|
|
|
|
rx.recv().unwrap();
|
|
|
|
t!(s.write(&[2]));
|
|
|
|
rx.recv().unwrap();
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut s1 = t!(acceptor.accept()).0;
|
|
|
|
let s2 = t!(s1.try_clone());
|
|
|
|
|
|
|
|
let (done, rx) = channel();
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
let mut s2 = s2;
|
|
|
|
let mut buf = [0, 0];
|
|
|
|
t!(s2.read(&mut buf));
|
|
|
|
tx2.send(()).unwrap();
|
|
|
|
done.send(()).unwrap();
|
|
|
|
});
|
|
|
|
let mut buf = [0, 0];
|
|
|
|
t!(s1.read(&mut buf));
|
|
|
|
tx1.send(()).unwrap();
|
|
|
|
|
|
|
|
rx.recv().unwrap();
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn tcp_clone_two_write() {
|
|
|
|
each_ip(&mut |addr| {
|
|
|
|
let acceptor = t!(TcpListener::bind(&addr));
|
|
|
|
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
let mut s = t!(TcpStream::connect(&addr));
|
|
|
|
let mut buf = [0, 1];
|
|
|
|
t!(s.read(&mut buf));
|
|
|
|
t!(s.read(&mut buf));
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut s1 = t!(acceptor.accept()).0;
|
|
|
|
let s2 = t!(s1.try_clone());
|
|
|
|
|
|
|
|
let (done, rx) = channel();
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
let mut s2 = s2;
|
|
|
|
t!(s2.write(&[1]));
|
|
|
|
done.send(()).unwrap();
|
|
|
|
});
|
|
|
|
t!(s1.write(&[2]));
|
|
|
|
|
|
|
|
rx.recv().unwrap();
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn shutdown_smoke() {
|
|
|
|
each_ip(&mut |addr| {
|
|
|
|
let a = t!(TcpListener::bind(&addr));
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
let mut c = t!(a.accept()).0;
|
|
|
|
let mut b = [0];
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(c.read(&mut b).unwrap(), 0);
|
2015-02-06 00:50:11 +00:00
|
|
|
t!(c.write(&[1]));
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut s = t!(TcpStream::connect(&addr));
|
|
|
|
t!(s.shutdown(Shutdown::Write));
|
|
|
|
assert!(s.write(&[1]).is_err());
|
|
|
|
let mut b = [0, 0];
|
|
|
|
assert_eq!(t!(s.read(&mut b)), 1);
|
|
|
|
assert_eq!(b[0], 1);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn close_readwrite_smoke() {
|
|
|
|
each_ip(&mut |addr| {
|
|
|
|
let a = t!(TcpListener::bind(&addr));
|
|
|
|
let (tx, rx) = channel::<()>();
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
let _s = t!(a.accept());
|
|
|
|
let _ = rx.recv();
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut b = [0];
|
|
|
|
let mut s = t!(TcpStream::connect(&addr));
|
|
|
|
let mut s2 = t!(s.try_clone());
|
|
|
|
|
|
|
|
// closing should prevent reads/writes
|
|
|
|
t!(s.shutdown(Shutdown::Write));
|
|
|
|
assert!(s.write(&[0]).is_err());
|
|
|
|
t!(s.shutdown(Shutdown::Read));
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(s.read(&mut b).unwrap(), 0);
|
2015-02-06 00:50:11 +00:00
|
|
|
|
|
|
|
// closing should affect previous handles
|
|
|
|
assert!(s2.write(&[0]).is_err());
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(s2.read(&mut b).unwrap(), 0);
|
2015-02-06 00:50:11 +00:00
|
|
|
|
|
|
|
// closing should affect new handles
|
|
|
|
let mut s3 = t!(s.try_clone());
|
|
|
|
assert!(s3.write(&[0]).is_err());
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(s3.read(&mut b).unwrap(), 0);
|
2015-02-06 00:50:11 +00:00
|
|
|
|
|
|
|
// make sure these don't die
|
|
|
|
let _ = s2.shutdown(Shutdown::Read);
|
|
|
|
let _ = s2.shutdown(Shutdown::Write);
|
|
|
|
let _ = s3.shutdown(Shutdown::Read);
|
|
|
|
let _ = s3.shutdown(Shutdown::Write);
|
|
|
|
drop(tx);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-01-06 06:58:37 +00:00
|
|
|
#[cfg(unix)] // test doesn't work on Windows, see #31657
|
2015-02-06 00:50:11 +00:00
|
|
|
fn close_read_wakes_up() {
|
|
|
|
each_ip(&mut |addr| {
|
|
|
|
let a = t!(TcpListener::bind(&addr));
|
|
|
|
let (tx1, rx) = channel::<()>();
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
let _s = t!(a.accept());
|
|
|
|
let _ = rx.recv();
|
|
|
|
});
|
|
|
|
|
|
|
|
let s = t!(TcpStream::connect(&addr));
|
|
|
|
let s2 = t!(s.try_clone());
|
|
|
|
let (tx, rx) = channel();
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
let mut s2 = s2;
|
|
|
|
assert_eq!(t!(s2.read(&mut [0])), 0);
|
|
|
|
tx.send(()).unwrap();
|
|
|
|
});
|
2015-05-08 15:12:29 +00:00
|
|
|
// this should wake up the child thread
|
2015-02-06 00:50:11 +00:00
|
|
|
t!(s.shutdown(Shutdown::Read));
|
|
|
|
|
|
|
|
// this test will never finish if the child doesn't wake up
|
|
|
|
rx.recv().unwrap();
|
|
|
|
drop(tx1);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn clone_while_reading() {
|
|
|
|
each_ip(&mut |addr| {
|
|
|
|
let accept = t!(TcpListener::bind(&addr));
|
|
|
|
|
2015-05-08 15:12:29 +00:00
|
|
|
// Enqueue a thread to write to a socket
|
2015-02-06 00:50:11 +00:00
|
|
|
let (tx, rx) = channel();
|
|
|
|
let (txdone, rxdone) = channel();
|
|
|
|
let txdone2 = txdone.clone();
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
let mut tcp = t!(TcpStream::connect(&addr));
|
|
|
|
rx.recv().unwrap();
|
|
|
|
t!(tcp.write(&[0]));
|
|
|
|
txdone2.send(()).unwrap();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Spawn off a reading clone
|
|
|
|
let tcp = t!(accept.accept()).0;
|
|
|
|
let tcp2 = t!(tcp.try_clone());
|
|
|
|
let txdone3 = txdone.clone();
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
let mut tcp2 = tcp2;
|
|
|
|
t!(tcp2.read(&mut [0]));
|
|
|
|
txdone3.send(()).unwrap();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Try to ensure that the reading clone is indeed reading
|
|
|
|
for _ in 0..50 {
|
2015-02-17 23:10:25 +00:00
|
|
|
thread::yield_now();
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// clone the handle again while it's reading, then let it finish the
|
|
|
|
// read.
|
|
|
|
let _ = t!(tcp.try_clone());
|
|
|
|
tx.send(()).unwrap();
|
|
|
|
rxdone.recv().unwrap();
|
|
|
|
rxdone.recv().unwrap();
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn clone_accept_smoke() {
|
|
|
|
each_ip(&mut |addr| {
|
|
|
|
let a = t!(TcpListener::bind(&addr));
|
|
|
|
let a2 = t!(a.try_clone());
|
|
|
|
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
let _ = TcpStream::connect(&addr);
|
|
|
|
});
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
let _ = TcpStream::connect(&addr);
|
|
|
|
});
|
|
|
|
|
|
|
|
t!(a.accept());
|
|
|
|
t!(a2.accept());
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn clone_accept_concurrent() {
|
|
|
|
each_ip(&mut |addr| {
|
|
|
|
let a = t!(TcpListener::bind(&addr));
|
|
|
|
let a2 = t!(a.try_clone());
|
|
|
|
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
let tx2 = tx.clone();
|
|
|
|
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
tx.send(t!(a.accept())).unwrap();
|
|
|
|
});
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
tx2.send(t!(a2.accept())).unwrap();
|
|
|
|
});
|
|
|
|
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
let _ = TcpStream::connect(&addr);
|
|
|
|
});
|
2015-02-17 23:10:25 +00:00
|
|
|
let _t = thread::spawn(move|| {
|
2015-02-06 00:50:11 +00:00
|
|
|
let _ = TcpStream::connect(&addr);
|
|
|
|
});
|
|
|
|
|
|
|
|
rx.recv().unwrap();
|
|
|
|
rx.recv().unwrap();
|
|
|
|
})
|
|
|
|
}
|
2015-05-04 01:01:25 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn debug() {
|
|
|
|
let name = if cfg!(windows) {"socket"} else {"fd"};
|
|
|
|
let socket_addr = next_test_ip4();
|
|
|
|
|
|
|
|
let listener = t!(TcpListener::bind(&socket_addr));
|
|
|
|
let listener_inner = listener.0.socket().as_inner();
|
|
|
|
let compare = format!("TcpListener {{ addr: {:?}, {}: {:?} }}",
|
|
|
|
socket_addr, name, listener_inner);
|
|
|
|
assert_eq!(format!("{:?}", listener), compare);
|
|
|
|
|
2015-06-07 17:50:13 +00:00
|
|
|
let stream = t!(TcpStream::connect(&("localhost",
|
2015-05-04 01:01:25 +00:00
|
|
|
socket_addr.port())));
|
|
|
|
let stream_inner = stream.0.socket().as_inner();
|
|
|
|
let compare = format!("TcpStream {{ addr: {:?}, \
|
|
|
|
peer: {:?}, {}: {:?} }}",
|
|
|
|
stream.local_addr().unwrap(),
|
|
|
|
stream.peer_addr().unwrap(),
|
|
|
|
name,
|
|
|
|
stream_inner);
|
|
|
|
assert_eq!(format!("{:?}", stream), compare);
|
|
|
|
}
|
2015-05-27 06:47:03 +00:00
|
|
|
|
2015-06-09 16:36:17 +00:00
|
|
|
// FIXME: re-enabled bitrig/openbsd tests once their socket timeout code
|
|
|
|
// no longer has rounding errors.
|
2015-07-01 03:37:11 +00:00
|
|
|
#[cfg_attr(any(target_os = "bitrig", target_os = "netbsd", target_os = "openbsd"), ignore)]
|
2015-05-27 06:47:03 +00:00
|
|
|
#[test]
|
|
|
|
fn timeouts() {
|
|
|
|
let addr = next_test_ip4();
|
|
|
|
let listener = t!(TcpListener::bind(&addr));
|
|
|
|
|
|
|
|
let stream = t!(TcpStream::connect(&("localhost", addr.port())));
|
|
|
|
let dur = Duration::new(15410, 0);
|
|
|
|
|
|
|
|
assert_eq!(None, t!(stream.read_timeout()));
|
|
|
|
|
|
|
|
t!(stream.set_read_timeout(Some(dur)));
|
|
|
|
assert_eq!(Some(dur), t!(stream.read_timeout()));
|
|
|
|
|
|
|
|
assert_eq!(None, t!(stream.write_timeout()));
|
|
|
|
|
|
|
|
t!(stream.set_write_timeout(Some(dur)));
|
|
|
|
assert_eq!(Some(dur), t!(stream.write_timeout()));
|
|
|
|
|
|
|
|
t!(stream.set_read_timeout(None));
|
|
|
|
assert_eq!(None, t!(stream.read_timeout()));
|
|
|
|
|
|
|
|
t!(stream.set_write_timeout(None));
|
|
|
|
assert_eq!(None, t!(stream.write_timeout()));
|
2015-12-18 12:29:49 +00:00
|
|
|
drop(listener);
|
2015-05-27 06:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_read_timeout() {
|
|
|
|
let addr = next_test_ip4();
|
|
|
|
let listener = t!(TcpListener::bind(&addr));
|
|
|
|
|
|
|
|
let mut stream = t!(TcpStream::connect(&("localhost", addr.port())));
|
2015-05-30 02:09:29 +00:00
|
|
|
t!(stream.set_read_timeout(Some(Duration::from_millis(1000))));
|
2015-05-27 06:47:03 +00:00
|
|
|
|
|
|
|
let mut buf = [0; 10];
|
2015-12-18 12:29:49 +00:00
|
|
|
let start = Instant::now();
|
|
|
|
let kind = stream.read(&mut buf).err().expect("expected error").kind();
|
|
|
|
assert!(kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut);
|
|
|
|
assert!(start.elapsed() > Duration::from_millis(400));
|
|
|
|
drop(listener);
|
2015-05-27 06:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_read_with_timeout() {
|
|
|
|
let addr = next_test_ip4();
|
|
|
|
let listener = t!(TcpListener::bind(&addr));
|
|
|
|
|
|
|
|
let mut stream = t!(TcpStream::connect(&("localhost", addr.port())));
|
2015-05-30 02:09:29 +00:00
|
|
|
t!(stream.set_read_timeout(Some(Duration::from_millis(1000))));
|
2015-05-27 06:47:03 +00:00
|
|
|
|
|
|
|
let mut other_end = t!(listener.accept()).0;
|
|
|
|
t!(other_end.write_all(b"hello world"));
|
|
|
|
|
|
|
|
let mut buf = [0; 11];
|
|
|
|
t!(stream.read(&mut buf));
|
|
|
|
assert_eq!(b"hello world", &buf[..]);
|
|
|
|
|
2015-12-18 12:29:49 +00:00
|
|
|
let start = Instant::now();
|
|
|
|
let kind = stream.read(&mut buf).err().expect("expected error").kind();
|
|
|
|
assert!(kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut);
|
|
|
|
assert!(start.elapsed() > Duration::from_millis(400));
|
|
|
|
drop(listener);
|
2015-05-27 06:47:03 +00:00
|
|
|
}
|
2016-02-27 22:15:19 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn nodelay() {
|
|
|
|
let addr = next_test_ip4();
|
|
|
|
let _listener = t!(TcpListener::bind(&addr));
|
|
|
|
|
|
|
|
let stream = t!(TcpStream::connect(&("localhost", addr.port())));
|
|
|
|
|
|
|
|
assert_eq!(false, t!(stream.nodelay()));
|
|
|
|
t!(stream.set_nodelay(true));
|
|
|
|
assert_eq!(true, t!(stream.nodelay()));
|
|
|
|
t!(stream.set_nodelay(false));
|
|
|
|
assert_eq!(false, t!(stream.nodelay()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ttl() {
|
|
|
|
let ttl = 100;
|
|
|
|
|
|
|
|
let addr = next_test_ip4();
|
|
|
|
let listener = t!(TcpListener::bind(&addr));
|
|
|
|
|
|
|
|
t!(listener.set_ttl(ttl));
|
|
|
|
assert_eq!(ttl, t!(listener.ttl()));
|
|
|
|
|
|
|
|
let stream = t!(TcpStream::connect(&("localhost", addr.port())));
|
|
|
|
|
|
|
|
t!(stream.set_ttl(ttl));
|
|
|
|
assert_eq!(ttl, t!(stream.ttl()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn set_nonblocking() {
|
|
|
|
let addr = next_test_ip4();
|
|
|
|
let listener = t!(TcpListener::bind(&addr));
|
|
|
|
|
|
|
|
t!(listener.set_nonblocking(true));
|
|
|
|
t!(listener.set_nonblocking(false));
|
|
|
|
|
2016-03-03 06:05:14 +00:00
|
|
|
let mut stream = t!(TcpStream::connect(&("localhost", addr.port())));
|
2016-02-27 22:15:19 +00:00
|
|
|
|
|
|
|
t!(stream.set_nonblocking(false));
|
2016-03-03 06:05:14 +00:00
|
|
|
t!(stream.set_nonblocking(true));
|
|
|
|
|
|
|
|
let mut buf = [0];
|
|
|
|
match stream.read(&mut buf) {
|
|
|
|
Ok(_) => panic!("expected error"),
|
|
|
|
Err(ref e) if e.kind() == ErrorKind::WouldBlock => {}
|
|
|
|
Err(e) => panic!("unexpected error {}", e),
|
|
|
|
}
|
2016-02-27 22:15:19 +00:00
|
|
|
}
|
2017-01-11 03:11:56 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn peek() {
|
|
|
|
each_ip(&mut |addr| {
|
|
|
|
let (txdone, rxdone) = channel();
|
|
|
|
|
|
|
|
let srv = t!(TcpListener::bind(&addr));
|
|
|
|
let _t = thread::spawn(move|| {
|
|
|
|
let mut cl = t!(srv.accept()).0;
|
|
|
|
cl.write(&[1,3,3,7]).unwrap();
|
|
|
|
t!(rxdone.recv());
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut c = t!(TcpStream::connect(&addr));
|
|
|
|
let mut b = [0; 10];
|
|
|
|
for _ in 1..3 {
|
|
|
|
let len = c.peek(&mut b).unwrap();
|
|
|
|
assert_eq!(len, 4);
|
|
|
|
}
|
|
|
|
let len = c.read(&mut b).unwrap();
|
|
|
|
assert_eq!(len, 4);
|
|
|
|
|
|
|
|
t!(c.set_nonblocking(true));
|
|
|
|
match c.peek(&mut b) {
|
|
|
|
Ok(_) => panic!("expected error"),
|
|
|
|
Err(ref e) if e.kind() == ErrorKind::WouldBlock => {}
|
|
|
|
Err(e) => panic!("unexpected error {}", e),
|
|
|
|
}
|
|
|
|
t!(txdone.send(()));
|
|
|
|
})
|
|
|
|
}
|
2017-07-05 06:46:24 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn connect_timeout_unroutable() {
|
2017-09-17 03:36:11 +00:00
|
|
|
// this IP is unroutable, so connections should always time out,
|
|
|
|
// provided the network is reachable to begin with.
|
2017-07-05 06:46:24 +00:00
|
|
|
let addr = "10.255.255.1:80".parse().unwrap();
|
|
|
|
let e = TcpStream::connect_timeout(&addr, Duration::from_millis(250)).unwrap_err();
|
2017-09-17 03:36:11 +00:00
|
|
|
assert!(e.kind() == io::ErrorKind::TimedOut ||
|
|
|
|
e.kind() == io::ErrorKind::Other,
|
|
|
|
"bad error: {} {:?}", e, e.kind());
|
2017-07-05 06:46:24 +00:00
|
|
|
}
|
|
|
|
|
2017-10-14 02:22:33 +00:00
|
|
|
#[test]
|
|
|
|
fn connect_timeout_unbound() {
|
|
|
|
// bind and drop a socket to track down a "probably unassigned" port
|
|
|
|
let socket = TcpListener::bind("127.0.0.1:0").unwrap();
|
|
|
|
let addr = socket.local_addr().unwrap();
|
|
|
|
drop(socket);
|
|
|
|
|
|
|
|
let timeout = Duration::from_secs(1);
|
|
|
|
let e = TcpStream::connect_timeout(&addr, timeout).unwrap_err();
|
|
|
|
assert!(e.kind() == io::ErrorKind::ConnectionRefused ||
|
|
|
|
e.kind() == io::ErrorKind::TimedOut ||
|
|
|
|
e.kind() == io::ErrorKind::Other,
|
|
|
|
"bad error: {} {:?}", e, e.kind());
|
|
|
|
}
|
|
|
|
|
2017-07-05 06:46:24 +00:00
|
|
|
#[test]
|
|
|
|
fn connect_timeout_valid() {
|
|
|
|
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
|
|
|
let addr = listener.local_addr().unwrap();
|
|
|
|
TcpStream::connect_timeout(&addr, Duration::from_secs(2)).unwrap();
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|