[−][src]Struct smol::net::TcpStream
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::*; 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
impl TcpStream
[src]
pub async fn connect<A>(addr: A) -> Result<TcpStream, Error> where
A: AsyncToSocketAddrs,
[src]
A: AsyncToSocketAddrs,
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?;
pub fn local_addr(&self) -> Result<SocketAddr, Error>
[src]
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()?);
pub fn peer_addr(&self) -> Result<SocketAddr, Error>
[src]
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()?);
pub fn shutdown(&self, how: Shutdown) -> Result<(), Error>
[src]
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)?;
pub async fn peek(&'_ self, buf: &'_ mut [u8]) -> Result<usize, Error>
[src]
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?;
pub fn nodelay(&self) -> Result<bool, Error>
[src]
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()?);
pub fn set_nodelay(&self, nodelay: bool) -> Result<(), Error>
[src]
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)?;
pub fn ttl(&self) -> Result<u32, Error>
[src]
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()?);
pub fn set_ttl(&self, ttl: u32) -> Result<(), Error>
[src]
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
impl AsRawFd for TcpStream
[src]
impl<'_> AsyncRead for &'_ TcpStream
[src]
fn poll_read(
self: Pin<&mut &'_ TcpStream>,
cx: &mut Context<'_>,
buf: &mut [u8]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut &'_ TcpStream>,
cx: &mut Context<'_>,
buf: &mut [u8]
) -> Poll<Result<usize, Error>>
fn poll_read_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &mut [IoSliceMut<'_>]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &mut [IoSliceMut<'_>]
) -> Poll<Result<usize, Error>>
impl AsyncRead for TcpStream
[src]
fn poll_read(
self: Pin<&mut TcpStream>,
cx: &mut Context<'_>,
buf: &mut [u8]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut TcpStream>,
cx: &mut Context<'_>,
buf: &mut [u8]
) -> Poll<Result<usize, Error>>
fn poll_read_vectored(
self: Pin<&mut TcpStream>,
cx: &mut Context<'_>,
bufs: &mut [IoSliceMut<'_>]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut TcpStream>,
cx: &mut Context<'_>,
bufs: &mut [IoSliceMut<'_>]
) -> Poll<Result<usize, Error>>
impl AsyncWrite for TcpStream
[src]
fn poll_write(
self: Pin<&mut TcpStream>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut TcpStream>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize, Error>>
fn poll_write_vectored(
self: Pin<&mut TcpStream>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut TcpStream>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>]
) -> Poll<Result<usize, Error>>
fn poll_flush(
self: Pin<&mut TcpStream>,
cx: &mut Context<'_>
) -> Poll<Result<(), Error>>
[src]
self: Pin<&mut TcpStream>,
cx: &mut Context<'_>
) -> Poll<Result<(), Error>>
fn poll_close(
self: Pin<&mut TcpStream>,
cx: &mut Context<'_>
) -> Poll<Result<(), Error>>
[src]
self: Pin<&mut TcpStream>,
cx: &mut Context<'_>
) -> Poll<Result<(), Error>>
impl<'_> AsyncWrite for &'_ TcpStream
[src]
fn poll_write(
self: Pin<&mut &'_ TcpStream>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut &'_ TcpStream>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize, Error>>
fn poll_flush(
self: Pin<&mut &'_ TcpStream>,
cx: &mut Context<'_>
) -> Poll<Result<(), Error>>
[src]
self: Pin<&mut &'_ TcpStream>,
cx: &mut Context<'_>
) -> Poll<Result<(), Error>>
fn poll_close(
self: Pin<&mut &'_ TcpStream>,
cx: &mut Context<'_>
) -> Poll<Result<(), Error>>
[src]
self: Pin<&mut &'_ TcpStream>,
cx: &mut Context<'_>
) -> Poll<Result<(), Error>>
fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>]
) -> Poll<Result<usize, Error>>
impl Clone for TcpStream
[src]
impl Debug for TcpStream
[src]
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
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<R> AsyncReadExt for R where
R: AsyncRead + ?Sized,
[src]
R: AsyncRead + ?Sized,
fn read(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>ⓘNotable traits for ReadFuture<'_, R>
impl<'_, R> Future for ReadFuture<'_, R> where
R: Unpin + AsyncRead + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
[src]
Notable traits for ReadFuture<'_, R>
impl<'_, R> Future for ReadFuture<'_, R> where
R: Unpin + AsyncRead + ?Sized, type Output = Result<usize, Error>;
Self: Unpin,
fn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self>ⓘNotable traits for ReadVectoredFuture<'_, R>
impl<'_, R> Future for ReadVectoredFuture<'_, R> where
R: Unpin + AsyncRead + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
[src]
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self>ⓘ
Notable traits for ReadVectoredFuture<'_, R>
impl<'_, R> Future for ReadVectoredFuture<'_, R> where
R: Unpin + AsyncRead + ?Sized, type Output = Result<usize, Error>;
Self: Unpin,
fn read_to_end(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEndFuture<'a, Self>ⓘNotable traits for ReadToEndFuture<'_, R>
impl<'_, R> Future for ReadToEndFuture<'_, R> where
R: Unpin + AsyncRead + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
[src]
Notable traits for ReadToEndFuture<'_, R>
impl<'_, R> Future for ReadToEndFuture<'_, R> where
R: Unpin + AsyncRead + ?Sized, type Output = Result<usize, Error>;
Self: Unpin,
fn read_to_string(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self>ⓘNotable traits for ReadToStringFuture<'_, R>
impl<'_, R> Future for ReadToStringFuture<'_, R> where
R: Unpin + AsyncRead + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
[src]
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self>ⓘ
Notable traits for ReadToStringFuture<'_, R>
impl<'_, R> Future for ReadToStringFuture<'_, R> where
R: Unpin + AsyncRead + ?Sized, type Output = Result<usize, Error>;
Self: Unpin,
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>ⓘNotable traits for ReadExactFuture<'_, R>
impl<'_, R> Future for ReadExactFuture<'_, R> where
R: Unpin + AsyncRead + ?Sized, type Output = Result<(), Error>;
where
Self: Unpin,
[src]
Notable traits for ReadExactFuture<'_, R>
impl<'_, R> Future for ReadExactFuture<'_, R> where
R: Unpin + AsyncRead + ?Sized, type Output = Result<(), Error>;
Self: Unpin,
fn take(self, limit: u64) -> Take<Self>
[src]
fn bytes(self) -> Bytes<Self>
[src]
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: AsyncRead,
[src]
R: AsyncRead,
impl<W> AsyncWriteExt for W where
W: AsyncWrite + ?Sized,
[src]
W: AsyncWrite + ?Sized,
fn write(&'a mut self, buf: &'a [u8]) -> WriteFuture<'a, Self>ⓘNotable traits for WriteFuture<'_, W>
impl<'_, W> Future for WriteFuture<'_, W> where
W: Unpin + AsyncWrite + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
[src]
Notable traits for WriteFuture<'_, W>
impl<'_, W> Future for WriteFuture<'_, W> where
W: Unpin + AsyncWrite + ?Sized, type Output = Result<usize, Error>;
Self: Unpin,
fn write_vectored(
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectoredFuture<'a, Self>ⓘNotable traits for WriteVectoredFuture<'_, W>
impl<'_, W> Future for WriteVectoredFuture<'_, W> where
W: Unpin + AsyncWrite + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
[src]
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectoredFuture<'a, Self>ⓘ
Notable traits for WriteVectoredFuture<'_, W>
impl<'_, W> Future for WriteVectoredFuture<'_, W> where
W: Unpin + AsyncWrite + ?Sized, type Output = Result<usize, Error>;
Self: Unpin,
fn write_all(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self>ⓘNotable traits for WriteAllFuture<'_, W>
impl<'_, W> Future for WriteAllFuture<'_, W> where
W: Unpin + AsyncWrite + ?Sized, type Output = Result<(), Error>;
where
Self: Unpin,
[src]
Notable traits for WriteAllFuture<'_, W>
impl<'_, W> Future for WriteAllFuture<'_, W> where
W: Unpin + AsyncWrite + ?Sized, type Output = Result<(), Error>;
Self: Unpin,
fn flush(&mut self) -> FlushFuture<'_, Self>ⓘNotable traits for FlushFuture<'_, W>
impl<'_, W> Future for FlushFuture<'_, W> where
W: Unpin + AsyncWrite + ?Sized, type Output = Result<(), Error>;
where
Self: Unpin,
[src]
Notable traits for FlushFuture<'_, W>
impl<'_, W> Future for FlushFuture<'_, W> where
W: Unpin + AsyncWrite + ?Sized, type Output = Result<(), Error>;
Self: Unpin,
fn close(&mut self) -> CloseFuture<'_, Self>ⓘNotable traits for CloseFuture<'_, W>
impl<'_, W> Future for CloseFuture<'_, W> where
W: Unpin + AsyncWrite + ?Sized, type Output = Result<(), Error>;
where
Self: Unpin,
[src]
Notable traits for CloseFuture<'_, W>
impl<'_, W> Future for CloseFuture<'_, W> where
W: Unpin + AsyncWrite + ?Sized, type Output = Result<(), Error>;
Self: Unpin,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut Tⓘ
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,