pub struct TcpStream { /* private fields */ }
Expand description
A TCP connection.
A TcpStream
can be created by connect
ing to an endpoint or by
accept
ing an incoming connection.
TcpStream
is a bidirectional stream that implements traits [AsyncRead
] and
[AsyncWrite
].
Cloning a TcpStream
creates another handle to the same socket. 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.
The Transmission Control Protocol is specified in IETF RFC 793.
Examples
use async_net::TcpStream;
use futures_lite::prelude::*;
let mut stream = TcpStream::connect("127.0.0.1:8080").await?;
stream.write_all(b"hello").await?;
let mut buf = vec![0u8; 1024];
let n = stream.read(&mut buf).await?;
Implementations§
source§impl TcpStream
impl TcpStream
sourcepub async fn connect<A: AsyncToSocketAddrs>(addr: A) -> Result<TcpStream>
pub async fn connect<A: AsyncToSocketAddrs>(addr: A) -> Result<TcpStream>
Creates a TCP connection to the specified address.
This method will create a new TCP socket and attempt to connect it to the provided addr
,
If addr
yields multiple addresses, connecting will be attempted with each of the
addresses until connecting to one succeeds. If none of the addresses result in a successful
connection, the error from the last connect attempt is returned.
Examples
Connect to example.com:80
:
use async_net::TcpStream;
let stream = TcpStream::connect("example.com:80").await?;
Connect to 127.0.0.1:8080
. If that fails, then try connecting to 127.0.0.1:8081
:
use async_net::{SocketAddr, TcpStream};
let addrs = [
SocketAddr::from(([127, 0, 0, 1], 8080)),
SocketAddr::from(([127, 0, 0, 1], 8081)),
];
let stream = TcpStream::connect(&addrs[..]).await?;
sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
Returns the local address this stream is bound to.
Examples
use async_net::TcpStream;
let stream = TcpStream::connect("example.com:80").await?;
println!("Local address is {}", stream.local_addr()?);
sourcepub fn peer_addr(&self) -> Result<SocketAddr>
pub fn peer_addr(&self) -> Result<SocketAddr>
Returns the remote address this stream is connected to.
Examples
use async_net::TcpStream;
let stream = TcpStream::connect("example.com:80").await?;
println!("Connected to {}", stream.peer_addr()?);
sourcepub fn shutdown(&self, how: Shutdown) -> Result<()>
pub 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_net::{Shutdown, TcpStream};
let stream = TcpStream::connect("127.0.0.1:8080").await?;
stream.shutdown(Shutdown::Both)?;
sourcepub async fn peek(&self, buf: &mut [u8]) -> Result<usize>
pub async fn peek(&self, buf: &mut [u8]) -> Result<usize>
Receives data without removing it 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 async_net::TcpStream;
let stream = TcpStream::connect("127.0.0.1:8080").await?;
let mut buf = vec![0; 1024];
let n = stream.peek(&mut buf).await?;
sourcepub fn nodelay(&self) -> Result<bool>
pub fn nodelay(&self) -> Result<bool>
Gets the value of the TCP_NODELAY
option for this socket.
If set to true
, this option disables the Nagle algorithm. This means that
written data is always sent as soon as possible, even if there is only a small amount of
it.
When set to false
, written data is buffered until there is a certain amount to send out,
thereby avoiding the frequent sending of small packets.
Examples
use async_net::TcpStream;
let stream = TcpStream::connect("127.0.0.1:8080").await?;
println!("TCP_NODELAY is set to {}", stream.nodelay()?);
sourcepub fn set_nodelay(&self, nodelay: bool) -> Result<()>
pub fn set_nodelay(&self, nodelay: bool) -> Result<()>
Sets the value of the TCP_NODELAY
option for this socket.
If set to true
, this option disables the Nagle algorithm. This means that
written data is always sent as soon as possible, even if there is only a small amount of
it.
When set to false
, written data is buffered until there is a certain amount to send out,
thereby avoiding the frequent sending of small packets.
Examples
use async_net::TcpStream;
let stream = TcpStream::connect("127.0.0.1:8080").await?;
stream.set_nodelay(false)?;
sourcepub fn ttl(&self) -> Result<u32>
pub fn ttl(&self) -> Result<u32>
Gets the value of the IP_TTL
option for this socket.
This option configures the time-to-live field that is used in every packet sent from this socket.
Examples
use async_net::TcpStream;
let stream = TcpStream::connect("127.0.0.1:8080").await?;
println!("IP_TTL is set to {}", stream.ttl()?);
sourcepub fn set_ttl(&self, ttl: u32) -> Result<()>
pub fn set_ttl(&self, ttl: u32) -> Result<()>
Sets the value of the IP_TTL
option for this socket.
This option configures the time-to-live field that is used in every packet sent from this socket.
Examples
use async_net::TcpStream;
let stream = TcpStream::connect("127.0.0.1:8080").await?;
stream.set_ttl(100)?;
Trait Implementations§
source§impl AsRawSocket for TcpStream
impl AsRawSocket for TcpStream
source§fn as_raw_socket(&self) -> RawSocket
fn as_raw_socket(&self) -> RawSocket
source§impl AsSocket for TcpStream
impl AsSocket for TcpStream
source§fn as_socket(&self) -> BorrowedSocket<'_>
fn as_socket(&self) -> BorrowedSocket<'_>
source§impl AsyncRead for TcpStream
impl AsyncRead for TcpStream
source§impl AsyncWrite for TcpStream
impl AsyncWrite for TcpStream
source§fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize>>
fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8] ) -> Poll<Result<usize>>
buf
into the object. Read moresource§fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
source§impl TryFrom<OwnedSocket> for TcpStream
impl TryFrom<OwnedSocket> for TcpStream
impl RefUnwindSafe for TcpStream
impl UnwindSafe for TcpStream
Auto Trait Implementations§
Blanket Implementations§
§impl<T> AsFd for Twhere
T: AsSocket,
impl<T> AsFd for Twhere T: AsSocket,
§fn as_fd(&self) -> BorrowedSocket<'_>
fn as_fd(&self) -> BorrowedSocket<'_>
as_fd
function for Winsock2, where a Fd
is a Socket
.§impl<T> AsRawFd for Twhere
T: AsRawSocket,
impl<T> AsRawFd for Twhere T: AsRawSocket,
§impl<T> AsRawSocketlike for Twhere
T: AsRawSocket,
impl<T> AsRawSocketlike for Twhere T: AsRawSocket,
§fn as_raw_socketlike(&self) -> u64
fn as_raw_socketlike(&self) -> u64
§impl<T> AsSocketlike for Twhere
T: AsSocket,
impl<T> AsSocketlike for Twhere T: AsSocket,
§fn as_socketlike(&self) -> BorrowedSocket<'_>
fn as_socketlike(&self) -> BorrowedSocket<'_>
§fn as_socketlike_view<Target>(&self) -> SocketlikeView<'_, Target>where
Target: SocketlikeViewType,
fn as_socketlike_view<Target>(&self) -> SocketlikeView<'_, Target>where Target: SocketlikeViewType,
&Target
. Read moresource§impl<R> AsyncReadExt for Rwhere
R: AsyncRead + ?Sized,
impl<R> AsyncReadExt for Rwhere R: AsyncRead + ?Sized,
source§fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where Self: Unpin,
source§fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
fn read_vectored<'a>( &'a mut self, bufs: &'a mut [IoSliceMut<'a>] ) -> ReadVectoredFuture<'a, Self>where Self: Unpin,
source§fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8, Global>
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
fn read_to_end<'a>( &'a mut self, buf: &'a mut Vec<u8, Global> ) -> ReadToEndFuture<'a, Self>where Self: Unpin,
source§fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
fn read_to_string<'a>( &'a mut self, buf: &'a mut String ) -> ReadToStringFuture<'a, Self>where Self: Unpin,
source§fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where Self: Unpin,
buf
. Read moresource§fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
fn take(self, limit: u64) -> Take<Self>where Self: Sized,
limit
bytes from it. Read more