pub struct TcpStream(_);
Expand description
A TCP stream between a local and a remote socket.
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.
Examples
use std::io::prelude::*;
use std::net::TcpStream;
fn main() -> std::io::Result<()> {
let mut stream = TcpStream::connect("127.0.0.1:34254")?;
stream.write(&[1])?;
stream.read(&mut [0; 128])?;
Ok(())
} // the stream is closed here
Implementations
sourceimpl TcpStream
impl TcpStream
sourcepub fn connect<A>(addr: A) -> Result<TcpStream, Error> where
A: ToSocketAddrs,
pub fn connect<A>(addr: A) -> Result<TcpStream, Error> where
A: ToSocketAddrs,
Opens a TCP connection to a remote host.
addr
is an address of the remote host. Anything which implements
ToSocketAddrs
trait can be supplied for the address; see this trait
documentation for concrete examples.
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.
Examples
Open a TCP connection to 127.0.0.1:8080
:
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...");
}
Open a TCP connection to 127.0.0.1:8080
. If the connection fails, open
a TCP connection to 127.0.0.1:8081
:
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...");
}
1.21.0 · sourcepub fn connect_timeout(
addr: &SocketAddr,
timeout: Duration
) -> Result<TcpStream, Error>
pub fn connect_timeout(
addr: &SocketAddr,
timeout: Duration
) -> Result<TcpStream, Error>
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.
sourcepub fn peer_addr(&self) -> Result<SocketAddr, Error>
pub fn peer_addr(&self) -> Result<SocketAddr, Error>
Returns the socket address of the remote peer of this TCP connection.
Examples
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)));
sourcepub fn local_addr(&self) -> Result<SocketAddr, Error>
pub fn local_addr(&self) -> Result<SocketAddr, Error>
Returns the socket address of the local half of this TCP connection.
Examples
use std::net::{IpAddr, Ipv4Addr, TcpStream};
let stream = TcpStream::connect("127.0.0.1:8080")
.expect("Couldn't connect to the server...");
assert_eq!(stream.local_addr().unwrap().ip(),
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
sourcepub fn shutdown(&self, how: Shutdown) -> Result<(), Error>
pub fn shutdown(&self, how: Shutdown) -> Result<(), Error>
Shuts down the read, write, or both halves of this connection.
This function will cause all pending and future I/O on the specified
portions to return immediately with an appropriate value (see the
documentation of Shutdown
).
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.
Examples
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");
sourcepub fn try_clone(&self) -> Result<TcpStream, Error>
pub fn try_clone(&self) -> Result<TcpStream, Error>
Creates a new independently owned handle to the underlying socket.
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.
Examples
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...");
1.4.0 · sourcepub fn set_read_timeout(&self, dur: Option<Duration>) -> Result<(), Error>
pub fn set_read_timeout(&self, dur: Option<Duration>) -> Result<(), Error>
Sets the read timeout to the timeout specified.
If the value specified is None
, then read
calls will block
indefinitely. An Err
is returned if the zero Duration
is
passed to this method.
Platform-specific behavior
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
error of the kind WouldBlock
, but Windows may return TimedOut
.
Examples
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");
An Err
is returned if the zero Duration
is passed to this
method:
use std::io;
use std::net::TcpStream;
use std::time::Duration;
let stream = TcpStream::connect("127.0.0.1:8080").unwrap();
let result = stream.set_read_timeout(Some(Duration::new(0, 0)));
let err = result.unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
1.4.0 · sourcepub fn set_write_timeout(&self, dur: Option<Duration>) -> Result<(), Error>
pub fn set_write_timeout(&self, dur: Option<Duration>) -> Result<(), Error>
Sets the write timeout to the timeout specified.
If the value specified is None
, then write
calls will block
indefinitely. An Err
is returned if the zero Duration
is
passed to this method.
Platform-specific behavior
Platforms may return a different error code whenever a write times out
as a result of setting this option. For example Unix typically returns
an error of the kind WouldBlock
, but Windows may return TimedOut
.
Examples
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");
An Err
is returned if the zero Duration
is passed to this
method:
use std::io;
use std::net::TcpStream;
use std::time::Duration;
let stream = TcpStream::connect("127.0.0.1:8080").unwrap();
let result = stream.set_write_timeout(Some(Duration::new(0, 0)));
let err = result.unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
1.4.0 · sourcepub fn read_timeout(&self) -> Result<Option<Duration>, Error>
pub fn read_timeout(&self) -> Result<Option<Duration>, Error>
Returns the read timeout of this socket.
If the timeout is None
, then read
calls will block indefinitely.
Platform-specific behavior
Some platforms do not provide access to the current timeout.
Examples
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);
1.4.0 · sourcepub fn write_timeout(&self) -> Result<Option<Duration>, Error>
pub fn write_timeout(&self) -> Result<Option<Duration>, Error>
Returns the write timeout of this socket.
If the timeout is None
, then write
calls will block indefinitely.
Platform-specific behavior
Some platforms do not provide access to the current timeout.
Examples
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);
1.18.0 · sourcepub fn peek(&self, buf: &mut [u8]) -> Result<usize, Error>
pub fn peek(&self, buf: &mut [u8]) -> Result<usize, Error>
Receives data on the socket from the remote address to which it is 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
use std::net::TcpStream;
let stream = TcpStream::connect("127.0.0.1:8000")
.expect("Couldn't connect to the server...");
let mut buf = [0; 10];
let len = stream.peek(&mut buf).expect("peek failed");
sourcepub fn set_linger(&self, linger: Option<Duration>) -> Result<(), Error>
🔬 This is a nightly-only experimental API. (tcp_linger
)
pub fn set_linger(&self, linger: Option<Duration>) -> Result<(), Error>
tcp_linger
)Sets the value of the SO_LINGER
option on this socket.
This value controls how the socket is closed when data remains
to be sent. If SO_LINGER
is set, the socket will remain open
for the specified duration as the system attempts to send pending data.
Otherwise, the system may close the socket immediately, or wait for a
default timeout.
Examples
#![feature(tcp_linger)]
use std::net::TcpStream;
use std::time::Duration;
let stream = TcpStream::connect("127.0.0.1:8080")
.expect("Couldn't connect to the server...");
stream.set_linger(Some(Duration::from_secs(0))).expect("set_linger call failed");
sourcepub fn linger(&self) -> Result<Option<Duration>, Error>
🔬 This is a nightly-only experimental API. (tcp_linger
)
pub fn linger(&self) -> Result<Option<Duration>, Error>
tcp_linger
)Gets the value of the SO_LINGER
option on this socket.
For more information about this option, see TcpStream::set_linger
.
Examples
#![feature(tcp_linger)]
use std::net::TcpStream;
use std::time::Duration;
let stream = TcpStream::connect("127.0.0.1:8080")
.expect("Couldn't connect to the server...");
stream.set_linger(Some(Duration::from_secs(0))).expect("set_linger call failed");
assert_eq!(stream.linger().unwrap(), Some(Duration::from_secs(0)));
1.9.0 · sourcepub fn set_nodelay(&self, nodelay: bool) -> Result<(), Error>
pub fn set_nodelay(&self, nodelay: bool) -> Result<(), Error>
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.
Examples
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");
1.9.0 · sourcepub fn nodelay(&self) -> Result<bool, Error>
pub fn nodelay(&self) -> Result<bool, Error>
Gets the value of the TCP_NODELAY
option on this socket.
For more information about this option, see TcpStream::set_nodelay
.
Examples
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);
1.9.0 · sourcepub fn set_ttl(&self, ttl: u32) -> Result<(), Error>
pub fn set_ttl(&self, ttl: u32) -> Result<(), Error>
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.
Examples
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");
1.9.0 · sourcepub fn ttl(&self) -> Result<u32, Error>
pub fn ttl(&self) -> Result<u32, Error>
Gets the value of the IP_TTL
option for this socket.
For more information about this option, see TcpStream::set_ttl
.
Examples
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);
1.9.0 · sourcepub fn take_error(&self) -> Result<Option<Error>, Error>
pub fn take_error(&self) -> Result<Option<Error>, Error>
Gets 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.
Examples
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...");
1.9.0 · sourcepub fn set_nonblocking(&self, nonblocking: bool) -> Result<(), Error>
pub fn set_nonblocking(&self, nonblocking: bool) -> Result<(), Error>
Moves this TCP stream into or out of nonblocking mode.
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
.
Examples
Reading bytes from a TCP stream in non-blocking mode:
use std::io::{self, Read};
use std::net::TcpStream;
let mut stream = TcpStream::connect("127.0.0.1:7878")
.expect("Couldn't connect to the server...");
stream.set_nonblocking(true).expect("set_nonblocking call failed");
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:?}");
Trait Implementations
sourceimpl AsRawSocket for TcpStream
impl AsRawSocket for TcpStream
sourcefn as_raw_socket(&self) -> u64
fn as_raw_socket(&self) -> u64
Extracts the raw socket. Read more
1.63.0 · sourceimpl AsSocket for TcpStream
impl AsSocket for TcpStream
sourcefn as_socket(&self) -> BorrowedSocket<'_>
fn as_socket(&self) -> BorrowedSocket<'_>
Borrows the socket.
sourceimpl AsTcpStream for TcpStream
impl AsTcpStream for TcpStream
1.63.0 · sourceimpl From<OwnedSocket> for TcpStream
impl From<OwnedSocket> for TcpStream
1.1.0 · sourceimpl FromRawSocket for TcpStream
impl FromRawSocket for TcpStream
1.4.0 · sourceimpl IntoRawSocket for TcpStream
impl IntoRawSocket for TcpStream
sourcefn into_raw_socket(self) -> u64
fn into_raw_socket(self) -> u64
Consumes this object, returning the raw underlying socket. Read more
sourceimpl Read for &TcpStream
impl Read for &TcpStream
sourcefn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>
Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
sourcefn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
Like read
, except that it reads into a slice of buffers. Read more
sourcefn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)Determines if this Read
er has an efficient read_vectored
implementation. Read more
sourcefn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
Read all bytes until EOF in this source, placing them into buf
. Read more
sourcefn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
Read all bytes until EOF in this source, appending them to buf
. Read more
1.6.0 · sourcefn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
Read the exact number of bytes required to fill buf
. Read more
sourcefn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
read_buf
)Pull some bytes from this source into the specified buffer. Read more
sourcefn read_buf_exact(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
read_buf
)Read the exact number of bytes required to fill buf
. Read more
sourcefn by_ref(&mut self) -> &mut Self
fn by_ref(&mut self) -> &mut Self
Creates a “by reference” adaptor for this instance of Read
. Read more
sourceimpl Read for TcpStream
impl Read for TcpStream
sourcefn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>
Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
sourcefn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
Like read
, except that it reads into a slice of buffers. Read more
sourcefn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)Determines if this Read
er has an efficient read_vectored
implementation. Read more
sourcefn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
Read all bytes until EOF in this source, placing them into buf
. Read more
sourcefn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
Read all bytes until EOF in this source, appending them to buf
. Read more
1.6.0 · sourcefn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
Read the exact number of bytes required to fill buf
. Read more
sourcefn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
read_buf
)Pull some bytes from this source into the specified buffer. Read more
sourcefn read_buf_exact(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
read_buf
)Read the exact number of bytes required to fill buf
. Read more
sourcefn by_ref(&mut self) -> &mut Self
fn by_ref(&mut self) -> &mut Self
Creates a “by reference” adaptor for this instance of Read
. Read more
sourceimpl Splittable for TcpStream
impl Splittable for TcpStream
sourceimpl TcpStreamExt for TcpStream
impl TcpStreamExt for TcpStream
sourcefn set_recv_buffer_size(&self, size: usize) -> Result<(), Error>
fn set_recv_buffer_size(&self, size: usize) -> Result<(), Error>
Sets the value of the SO_RCVBUF
option on this socket. Read more
sourcefn recv_buffer_size(&self) -> Result<usize, Error>
fn recv_buffer_size(&self) -> Result<usize, Error>
Gets the value of the SO_RCVBUF
option on this socket. Read more
sourcefn set_send_buffer_size(&self, size: usize) -> Result<(), Error>
fn set_send_buffer_size(&self, size: usize) -> Result<(), Error>
Sets the value of the SO_SNDBUF
option on this socket. Read more
sourcefn send_buffer_size(&self) -> Result<usize, Error>
fn send_buffer_size(&self) -> Result<usize, Error>
Gets the value of the SO_SNDBUF
option on this socket. Read more
sourcefn set_nodelay(&self, nodelay: bool) -> Result<(), Error>
fn set_nodelay(&self, nodelay: bool) -> Result<(), Error>
Sets the value of the TCP_NODELAY
option on this socket. Read more
sourcefn nodelay(&self) -> Result<bool, Error>
fn nodelay(&self) -> Result<bool, Error>
Gets the value of the TCP_NODELAY
option on this socket. Read more
sourcefn set_keepalive(&self, keepalive: Option<Duration>) -> Result<(), Error>
fn set_keepalive(&self, keepalive: Option<Duration>) -> Result<(), Error>
Sets whether keepalive messages are enabled to be sent on this socket. Read more
sourcefn keepalive(&self) -> Result<Option<Duration>, Error>
fn keepalive(&self) -> Result<Option<Duration>, Error>
Returns whether keepalive messages are enabled on this socket, and if so the duration of time between them. Read more
sourcefn set_keepalive_ms(&self, keepalive: Option<u32>) -> Result<(), Error>
fn set_keepalive_ms(&self, keepalive: Option<u32>) -> Result<(), Error>
Sets whether keepalive messages are enabled to be sent on this socket. Read more
sourcefn keepalive_ms(&self) -> Result<Option<u32>, Error>
fn keepalive_ms(&self) -> Result<Option<u32>, Error>
Returns whether keepalive messages are enabled on this socket, and if so the amount of milliseconds between them. Read more
sourcefn set_read_timeout_ms(&self, dur: Option<u32>) -> Result<(), Error>
fn set_read_timeout_ms(&self, dur: Option<u32>) -> Result<(), Error>
Sets the SO_RCVTIMEO
option for this socket. Read more
sourcefn read_timeout_ms(&self) -> Result<Option<u32>, Error>
fn read_timeout_ms(&self) -> Result<Option<u32>, Error>
Gets the value of the SO_RCVTIMEO
option for this socket. Read more
sourcefn set_write_timeout_ms(&self, dur: Option<u32>) -> Result<(), Error>
fn set_write_timeout_ms(&self, dur: Option<u32>) -> Result<(), Error>
Sets the SO_SNDTIMEO
option for this socket. Read more
sourcefn write_timeout_ms(&self) -> Result<Option<u32>, Error>
fn write_timeout_ms(&self) -> Result<Option<u32>, Error>
Gets the value of the SO_SNDTIMEO
option for this socket. Read more
sourcefn set_read_timeout(&self, dur: Option<Duration>) -> Result<(), Error>
fn set_read_timeout(&self, dur: Option<Duration>) -> Result<(), Error>
Sets the SO_RCVTIMEO
option for this socket. Read more
sourcefn read_timeout(&self) -> Result<Option<Duration>, Error>
fn read_timeout(&self) -> Result<Option<Duration>, Error>
Gets the value of the SO_RCVTIMEO
option for this socket. Read more
sourcefn set_write_timeout(&self, dur: Option<Duration>) -> Result<(), Error>
fn set_write_timeout(&self, dur: Option<Duration>) -> Result<(), Error>
Sets the SO_SNDTIMEO
option for this socket. Read more
sourcefn write_timeout(&self) -> Result<Option<Duration>, Error>
fn write_timeout(&self) -> Result<Option<Duration>, Error>
Gets the value of the SO_SNDTIMEO
option for this socket. Read more
sourcefn set_ttl(&self, ttl: u32) -> Result<(), Error>
fn set_ttl(&self, ttl: u32) -> Result<(), Error>
Sets the value for the IP_TTL
option on this socket. Read more
sourcefn ttl(&self) -> Result<u32, Error>
fn ttl(&self) -> Result<u32, Error>
Gets the value of the IP_TTL
option for this socket. Read more
sourcefn set_only_v6(&self, only_v6: bool) -> Result<(), Error>
fn set_only_v6(&self, only_v6: bool) -> Result<(), Error>
Sets the value for the IPV6_V6ONLY
option on this socket. Read more
sourcefn only_v6(&self) -> Result<bool, Error>
fn only_v6(&self) -> Result<bool, Error>
Gets the value of the IPV6_V6ONLY
option for this socket. Read more
sourcefn connect<T>(&self, addr: T) -> Result<(), Error> where
T: ToSocketAddrs,
fn connect<T>(&self, addr: T) -> Result<(), Error> where
T: ToSocketAddrs,
Executes a connect
operation on this socket, establishing a connection
to the host specified by addr
. Read more
sourcefn take_error(&self) -> Result<Option<Error>, Error>
fn take_error(&self) -> Result<Option<Error>, Error>
Get the value of the SO_ERROR
option on this socket. Read more
sourcefn set_nonblocking(&self, nonblocking: bool) -> Result<(), Error>
fn set_nonblocking(&self, nonblocking: bool) -> Result<(), Error>
Moves this TCP stream into or out of nonblocking mode. Read more
sourceimpl TcpStreamExt for TcpStream
impl TcpStreamExt for TcpStream
sourceunsafe fn read_overlapped(
&self,
buf: &mut [u8],
overlapped: *mut OVERLAPPED
) -> Result<Option<usize>, Error>
unsafe fn read_overlapped(
&self,
buf: &mut [u8],
overlapped: *mut OVERLAPPED
) -> Result<Option<usize>, Error>
Execute an overlapped read I/O operation on this TCP stream. Read more
sourceunsafe fn write_overlapped(
&self,
buf: &[u8],
overlapped: *mut OVERLAPPED
) -> Result<Option<usize>, Error>
unsafe fn write_overlapped(
&self,
buf: &[u8],
overlapped: *mut OVERLAPPED
) -> Result<Option<usize>, Error>
Execute an overlapped write I/O operation on this TCP stream. Read more
sourceunsafe fn connect_overlapped(
&self,
addr: &SocketAddr,
buf: &[u8],
overlapped: *mut OVERLAPPED
) -> Result<Option<usize>, Error>
unsafe fn connect_overlapped(
&self,
addr: &SocketAddr,
buf: &[u8],
overlapped: *mut OVERLAPPED
) -> Result<Option<usize>, Error>
Execute a connection operation for this socket. Read more
sourceimpl Write for &TcpStream
impl Write for &TcpStream
sourcefn write(&mut self, buf: &[u8]) -> Result<usize, Error>
fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
Write a buffer into this writer, returning how many bytes were written. Read more
sourcefn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)Determines if this Write
r has an efficient write_vectored
implementation. Read more
sourcefn flush(&mut self) -> Result<(), Error>
fn flush(&mut self) -> Result<(), Error>
Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
sourcefn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Attempts to write an entire buffer into this writer. Read more
sourcefn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)Attempts to write multiple buffers into this writer. Read more
sourceimpl Write for TcpStream
impl Write for TcpStream
sourcefn write(&mut self, buf: &[u8]) -> Result<usize, Error>
fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
Write a buffer into this writer, returning how many bytes were written. Read more
sourcefn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)Determines if this Write
r has an efficient write_vectored
implementation. Read more
sourcefn flush(&mut self) -> Result<(), Error>
fn flush(&mut self) -> Result<(), Error>
Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
sourcefn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Attempts to write an entire buffer into this writer. Read more
sourcefn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)Attempts to write multiple buffers into this writer. Read more
Auto Trait Implementations
impl RefUnwindSafe for TcpStream
impl Send for TcpStream
impl Sync for TcpStream
impl Unpin for TcpStream
impl UnwindSafe for TcpStream
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<R> ReadBytesExt for R where
R: Read + ?Sized,
impl<R> ReadBytesExt for R where
R: Read + ?Sized,
sourcefn read_u8(&mut self) -> Result<u8, Error>
fn read_u8(&mut self) -> Result<u8, Error>
Reads an unsigned 8 bit integer from the underlying reader. Read more
sourcefn read_i8(&mut self) -> Result<i8, Error>
fn read_i8(&mut self) -> Result<i8, Error>
Reads a signed 8 bit integer from the underlying reader. Read more
sourcefn read_u16<T>(&mut self) -> Result<u16, Error> where
T: ByteOrder,
fn read_u16<T>(&mut self) -> Result<u16, Error> where
T: ByteOrder,
Reads an unsigned 16 bit integer from the underlying reader. Read more
sourcefn read_i16<T>(&mut self) -> Result<i16, Error> where
T: ByteOrder,
fn read_i16<T>(&mut self) -> Result<i16, Error> where
T: ByteOrder,
Reads a signed 16 bit integer from the underlying reader. Read more
sourcefn read_u24<T>(&mut self) -> Result<u32, Error> where
T: ByteOrder,
fn read_u24<T>(&mut self) -> Result<u32, Error> where
T: ByteOrder,
Reads an unsigned 24 bit integer from the underlying reader. Read more
sourcefn read_i24<T>(&mut self) -> Result<i32, Error> where
T: ByteOrder,
fn read_i24<T>(&mut self) -> Result<i32, Error> where
T: ByteOrder,
Reads a signed 24 bit integer from the underlying reader. Read more
sourcefn read_u32<T>(&mut self) -> Result<u32, Error> where
T: ByteOrder,
fn read_u32<T>(&mut self) -> Result<u32, Error> where
T: ByteOrder,
Reads an unsigned 32 bit integer from the underlying reader. Read more
sourcefn read_i32<T>(&mut self) -> Result<i32, Error> where
T: ByteOrder,
fn read_i32<T>(&mut self) -> Result<i32, Error> where
T: ByteOrder,
Reads a signed 32 bit integer from the underlying reader. Read more
sourcefn read_u48<T>(&mut self) -> Result<u64, Error> where
T: ByteOrder,
fn read_u48<T>(&mut self) -> Result<u64, Error> where
T: ByteOrder,
Reads an unsigned 48 bit integer from the underlying reader. Read more
sourcefn read_i48<T>(&mut self) -> Result<i64, Error> where
T: ByteOrder,
fn read_i48<T>(&mut self) -> Result<i64, Error> where
T: ByteOrder,
Reads a signed 48 bit integer from the underlying reader. Read more
sourcefn read_u64<T>(&mut self) -> Result<u64, Error> where
T: ByteOrder,
fn read_u64<T>(&mut self) -> Result<u64, Error> where
T: ByteOrder,
Reads an unsigned 64 bit integer from the underlying reader. Read more
sourcefn read_i64<T>(&mut self) -> Result<i64, Error> where
T: ByteOrder,
fn read_i64<T>(&mut self) -> Result<i64, Error> where
T: ByteOrder,
Reads a signed 64 bit integer from the underlying reader. Read more
sourcefn read_u128<T>(&mut self) -> Result<u128, Error> where
T: ByteOrder,
fn read_u128<T>(&mut self) -> Result<u128, Error> where
T: ByteOrder,
Reads an unsigned 128 bit integer from the underlying reader. Read more
sourcefn read_i128<T>(&mut self) -> Result<i128, Error> where
T: ByteOrder,
fn read_i128<T>(&mut self) -> Result<i128, Error> where
T: ByteOrder,
Reads a signed 128 bit integer from the underlying reader. Read more
sourcefn read_uint<T>(&mut self, nbytes: usize) -> Result<u64, Error> where
T: ByteOrder,
fn read_uint<T>(&mut self, nbytes: usize) -> Result<u64, Error> where
T: ByteOrder,
Reads an unsigned n-bytes integer from the underlying reader. Read more
sourcefn read_int<T>(&mut self, nbytes: usize) -> Result<i64, Error> where
T: ByteOrder,
fn read_int<T>(&mut self, nbytes: usize) -> Result<i64, Error> where
T: ByteOrder,
Reads a signed n-bytes integer from the underlying reader. Read more
sourcefn read_uint128<T>(&mut self, nbytes: usize) -> Result<u128, Error> where
T: ByteOrder,
fn read_uint128<T>(&mut self, nbytes: usize) -> Result<u128, Error> where
T: ByteOrder,
Reads an unsigned n-bytes integer from the underlying reader.
sourcefn read_int128<T>(&mut self, nbytes: usize) -> Result<i128, Error> where
T: ByteOrder,
fn read_int128<T>(&mut self, nbytes: usize) -> Result<i128, Error> where
T: ByteOrder,
Reads a signed n-bytes integer from the underlying reader.
sourcefn read_f32<T>(&mut self) -> Result<f32, Error> where
T: ByteOrder,
fn read_f32<T>(&mut self) -> Result<f32, Error> where
T: ByteOrder,
Reads a IEEE754 single-precision (4 bytes) floating point number from the underlying reader. Read more
sourcefn read_f64<T>(&mut self) -> Result<f64, Error> where
T: ByteOrder,
fn read_f64<T>(&mut self) -> Result<f64, Error> where
T: ByteOrder,
Reads a IEEE754 double-precision (8 bytes) floating point number from the underlying reader. Read more
sourcefn read_u16_into<T>(&mut self, dst: &mut [u16]) -> Result<(), Error> where
T: ByteOrder,
fn read_u16_into<T>(&mut self, dst: &mut [u16]) -> Result<(), Error> where
T: ByteOrder,
Reads a sequence of unsigned 16 bit integers from the underlying reader. Read more
sourcefn read_u32_into<T>(&mut self, dst: &mut [u32]) -> Result<(), Error> where
T: ByteOrder,
fn read_u32_into<T>(&mut self, dst: &mut [u32]) -> Result<(), Error> where
T: ByteOrder,
Reads a sequence of unsigned 32 bit integers from the underlying reader. Read more
sourcefn read_u64_into<T>(&mut self, dst: &mut [u64]) -> Result<(), Error> where
T: ByteOrder,
fn read_u64_into<T>(&mut self, dst: &mut [u64]) -> Result<(), Error> where
T: ByteOrder,
Reads a sequence of unsigned 64 bit integers from the underlying reader. Read more
sourcefn read_u128_into<T>(&mut self, dst: &mut [u128]) -> Result<(), Error> where
T: ByteOrder,
fn read_u128_into<T>(&mut self, dst: &mut [u128]) -> Result<(), Error> where
T: ByteOrder,
Reads a sequence of unsigned 128 bit integers from the underlying reader. Read more
sourcefn read_i8_into(&mut self, dst: &mut [i8]) -> Result<(), Error>
fn read_i8_into(&mut self, dst: &mut [i8]) -> Result<(), Error>
Reads a sequence of signed 8 bit integers from the underlying reader. Read more
sourcefn read_i16_into<T>(&mut self, dst: &mut [i16]) -> Result<(), Error> where
T: ByteOrder,
fn read_i16_into<T>(&mut self, dst: &mut [i16]) -> Result<(), Error> where
T: ByteOrder,
Reads a sequence of signed 16 bit integers from the underlying reader. Read more
sourcefn read_i32_into<T>(&mut self, dst: &mut [i32]) -> Result<(), Error> where
T: ByteOrder,
fn read_i32_into<T>(&mut self, dst: &mut [i32]) -> Result<(), Error> where
T: ByteOrder,
Reads a sequence of signed 32 bit integers from the underlying reader. Read more
sourcefn read_i64_into<T>(&mut self, dst: &mut [i64]) -> Result<(), Error> where
T: ByteOrder,
fn read_i64_into<T>(&mut self, dst: &mut [i64]) -> Result<(), Error> where
T: ByteOrder,
Reads a sequence of signed 64 bit integers from the underlying reader. Read more
sourcefn read_i128_into<T>(&mut self, dst: &mut [i128]) -> Result<(), Error> where
T: ByteOrder,
fn read_i128_into<T>(&mut self, dst: &mut [i128]) -> Result<(), Error> where
T: ByteOrder,
Reads a sequence of signed 128 bit integers from the underlying reader. Read more
sourcefn read_f32_into<T>(&mut self, dst: &mut [f32]) -> Result<(), Error> where
T: ByteOrder,
fn read_f32_into<T>(&mut self, dst: &mut [f32]) -> Result<(), Error> where
T: ByteOrder,
Reads a sequence of IEEE754 single-precision (4 bytes) floating point numbers from the underlying reader. Read more
sourcefn read_f32_into_unchecked<T>(&mut self, dst: &mut [f32]) -> Result<(), Error> where
T: ByteOrder,
fn read_f32_into_unchecked<T>(&mut self, dst: &mut [f32]) -> Result<(), Error> where
T: ByteOrder,
please use read_f32_into
instead
DEPRECATED. Read more
sourceimpl<W> WriteBytesExt for W where
W: Write + ?Sized,
impl<W> WriteBytesExt for W where
W: Write + ?Sized,
sourcefn write_u8(&mut self, n: u8) -> Result<(), Error>
fn write_u8(&mut self, n: u8) -> Result<(), Error>
Writes an unsigned 8 bit integer to the underlying writer. Read more
sourcefn write_i8(&mut self, n: i8) -> Result<(), Error>
fn write_i8(&mut self, n: i8) -> Result<(), Error>
Writes a signed 8 bit integer to the underlying writer. Read more
sourcefn write_u16<T>(&mut self, n: u16) -> Result<(), Error> where
T: ByteOrder,
fn write_u16<T>(&mut self, n: u16) -> Result<(), Error> where
T: ByteOrder,
Writes an unsigned 16 bit integer to the underlying writer. Read more
sourcefn write_i16<T>(&mut self, n: i16) -> Result<(), Error> where
T: ByteOrder,
fn write_i16<T>(&mut self, n: i16) -> Result<(), Error> where
T: ByteOrder,
Writes a signed 16 bit integer to the underlying writer. Read more
sourcefn write_u24<T>(&mut self, n: u32) -> Result<(), Error> where
T: ByteOrder,
fn write_u24<T>(&mut self, n: u32) -> Result<(), Error> where
T: ByteOrder,
Writes an unsigned 24 bit integer to the underlying writer. Read more
sourcefn write_i24<T>(&mut self, n: i32) -> Result<(), Error> where
T: ByteOrder,
fn write_i24<T>(&mut self, n: i32) -> Result<(), Error> where
T: ByteOrder,
Writes a signed 24 bit integer to the underlying writer. Read more
sourcefn write_u32<T>(&mut self, n: u32) -> Result<(), Error> where
T: ByteOrder,
fn write_u32<T>(&mut self, n: u32) -> Result<(), Error> where
T: ByteOrder,
Writes an unsigned 32 bit integer to the underlying writer. Read more
sourcefn write_i32<T>(&mut self, n: i32) -> Result<(), Error> where
T: ByteOrder,
fn write_i32<T>(&mut self, n: i32) -> Result<(), Error> where
T: ByteOrder,
Writes a signed 32 bit integer to the underlying writer. Read more
sourcefn write_u48<T>(&mut self, n: u64) -> Result<(), Error> where
T: ByteOrder,
fn write_u48<T>(&mut self, n: u64) -> Result<(), Error> where
T: ByteOrder,
Writes an unsigned 48 bit integer to the underlying writer. Read more
sourcefn write_i48<T>(&mut self, n: i64) -> Result<(), Error> where
T: ByteOrder,
fn write_i48<T>(&mut self, n: i64) -> Result<(), Error> where
T: ByteOrder,
Writes a signed 48 bit integer to the underlying writer. Read more
sourcefn write_u64<T>(&mut self, n: u64) -> Result<(), Error> where
T: ByteOrder,
fn write_u64<T>(&mut self, n: u64) -> Result<(), Error> where
T: ByteOrder,
Writes an unsigned 64 bit integer to the underlying writer. Read more
sourcefn write_i64<T>(&mut self, n: i64) -> Result<(), Error> where
T: ByteOrder,
fn write_i64<T>(&mut self, n: i64) -> Result<(), Error> where
T: ByteOrder,
Writes a signed 64 bit integer to the underlying writer. Read more
sourcefn write_u128<T>(&mut self, n: u128) -> Result<(), Error> where
T: ByteOrder,
fn write_u128<T>(&mut self, n: u128) -> Result<(), Error> where
T: ByteOrder,
Writes an unsigned 128 bit integer to the underlying writer.
sourcefn write_i128<T>(&mut self, n: i128) -> Result<(), Error> where
T: ByteOrder,
fn write_i128<T>(&mut self, n: i128) -> Result<(), Error> where
T: ByteOrder,
Writes a signed 128 bit integer to the underlying writer.
sourcefn write_uint<T>(&mut self, n: u64, nbytes: usize) -> Result<(), Error> where
T: ByteOrder,
fn write_uint<T>(&mut self, n: u64, nbytes: usize) -> Result<(), Error> where
T: ByteOrder,
Writes an unsigned n-bytes integer to the underlying writer. Read more
sourcefn write_int<T>(&mut self, n: i64, nbytes: usize) -> Result<(), Error> where
T: ByteOrder,
fn write_int<T>(&mut self, n: i64, nbytes: usize) -> Result<(), Error> where
T: ByteOrder,
Writes a signed n-bytes integer to the underlying writer. Read more
sourcefn write_uint128<T>(&mut self, n: u128, nbytes: usize) -> Result<(), Error> where
T: ByteOrder,
fn write_uint128<T>(&mut self, n: u128, nbytes: usize) -> Result<(), Error> where
T: ByteOrder,
Writes an unsigned n-bytes integer to the underlying writer. Read more
sourcefn write_int128<T>(&mut self, n: i128, nbytes: usize) -> Result<(), Error> where
T: ByteOrder,
fn write_int128<T>(&mut self, n: i128, nbytes: usize) -> Result<(), Error> where
T: ByteOrder,
Writes a signed n-bytes integer to the underlying writer. Read more