pub trait TcpStream:
AsyncPeek
+ AsyncRead
+ AsyncWrite
+ Sized {
// Required methods
fn connect(addr: SocketAddr) -> Connect<Self> ⓘ;
fn local_addr(&self) -> Result<SocketAddr>;
fn peer_addr(&self) -> Result<SocketAddr>;
fn shutdown(&self, how: Shutdown) -> Result<()>;
fn nodelay(&self) -> Result<bool>;
fn set_nodelay(&self, nodelay: bool) -> Result<()>;
fn ttl(&self) -> Result<u32>;
fn set_ttl(&self, ttl: u32) -> Result<()>;
}
Expand description
A TCP stream between a local and a remote socket.
A TcpStream
can be created by either connect
ing to an endpoint or accept
ing a
connection on a TcpListener
.
TcpStream
is a bidirectional stream that implements AsyncPeek
, AsyncRead
and
AsyncWrite
.
The socket will be closed when all handles to it are dropped. The reading and writing portions
of the connection can also be shut down individually with the shutdown()
method.
Required Methods§
Sourcefn connect(addr: SocketAddr) -> Connect<Self> ⓘ
fn connect(addr: SocketAddr) -> Connect<Self> ⓘ
Opens a TCP connection to the specified address.
§Examples
use async_tcp::TcpStream;
use std::net::SocketAddr;
use std::str::FromStr;
// let addr = ...;
let stream = async_net::TcpStream::connect(addr).await?;
assert_eq!(addr, stream.peer_addr()?);
Sourcefn local_addr(&self) -> Result<SocketAddr>
fn local_addr(&self) -> Result<SocketAddr>
Returns the socket address of the local half of this TCP connection.
§Examples
use async_tcp::TcpStream;
use std::net::{IpAddr, SocketAddr};
use std::str::FromStr;
// let addr = ...;
let stream = async_net::TcpStream::connect(addr).await?;
assert_eq!(stream.local_addr()?.ip(), IpAddr::from_str("127.0.0.1").unwrap());
Sourcefn peer_addr(&self) -> Result<SocketAddr>
fn peer_addr(&self) -> Result<SocketAddr>
Returns the socket address of the remote peer of this TCP connection.
§Examples
use async_tcp::TcpStream;
use std::net::SocketAddr;
use std::str::FromStr;
// let addr = ...;
let stream = async_net::TcpStream::connect(addr).await?;
assert_eq!(addr, stream.peer_addr()?);
Sourcefn shutdown(&self, how: Shutdown) -> Result<()>
fn shutdown(&self, how: Shutdown) -> Result<()>
Shuts down the read half, write half, or both halves of this connection.
This method will cause all pending and future I/O in the given directions to return
immediately with an appropriate value (see the documentation of Shutdown
).
§Examples
use async_tcp::TcpStream;
use std::net::{Shutdown, SocketAddr};
use std::str::FromStr;
// let addr = ...;
let stream = async_net::TcpStream::connect(addr).await?;
stream.shutdown(Shutdown::Both)?;
Sourcefn nodelay(&self) -> Result<bool>
fn nodelay(&self) -> Result<bool>
Gets the value of the TCP_NODELAY
option on this socket.
For more information about this option, see set_nodelay()
.
§Examples
use async_tcp::TcpStream;
use std::net::SocketAddr;
use std::str::FromStr;
// let addr = ...;
let stream = async_net::TcpStream::connect(addr).await?;
stream.set_nodelay(true)?;
assert_eq!(stream.nodelay()?, true);
Sourcefn set_nodelay(&self, nodelay: bool) -> Result<()>
fn set_nodelay(&self, nodelay: bool) -> Result<()>
Sets the value of 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 reading of small packets.
§Examples
use async_tcp::TcpStream;
use std::net::SocketAddr;
use std::str::FromStr;
// let addr = ...;
let stream = async_net::TcpStream::connect(addr).await?;
stream.set_nodelay(true)?;
assert_eq!(stream.nodelay()?, true);
Sourcefn ttl(&self) -> Result<u32>
fn ttl(&self) -> Result<u32>
Gets the value of the IP_TTL
option on this socket.
For more information about this option, see set_ttl()
.
§Examples
use async_tcp::TcpStream;
use std::net::SocketAddr;
use std::str::FromStr;
// let addr = ...;
let stream = async_net::TcpStream::connect(addr).await?;
stream.set_ttl(100)?;
assert_eq!(stream.ttl()?, 100);
Sourcefn set_ttl(&self, ttl: u32) -> Result<()>
fn set_ttl(&self, ttl: u32) -> Result<()>
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 async_tcp::TcpStream;
use std::net::SocketAddr;
use std::str::FromStr;
// let addr = ...;
let stream = async_net::TcpStream::connect(addr).await?;
stream.set_ttl(100)?;
assert_eq!(stream.ttl()?, 100);
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.