Struct async_std::net::TcpStream

source ·
pub struct TcpStream { /* private fields */ }
Expand description

A TCP stream between a local and a remote socket.

A TcpStream can either be created by connecting to an endpoint, via the connect method, or by accepting a connection from a listener. It can be read or written to using the AsyncRead, AsyncWrite, and related extension traits in futures::io.

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.

This type is an async version of std::net::TcpStream.

Examples

use async_std::net::TcpStream;
use async_std::prelude::*;

let mut stream = TcpStream::connect("127.0.0.1:8080").await?;
stream.write_all(b"hello world").await?;

let mut buf = vec![0u8; 1024];
let n = stream.read(&mut buf).await?;

Implementations§

source§

impl TcpStream

source

pub async fn connect<A: ToSocketAddrs>(addrs: A) -> Result<TcpStream>

Creates a new TCP stream connected to the specified address.

This method will create a new TCP socket and attempt to connect it to the addr provided. The returned future will be resolved once the stream has successfully connected, or it will return an error if one occurs.

Examples
use async_std::net::TcpStream;

let stream = TcpStream::connect("127.0.0.1:0").await?;
source

pub fn local_addr(&self) -> Result<SocketAddr>

Returns the local address that this stream is connected to.

Examples
use async_std::net::TcpStream;

let stream = TcpStream::connect("127.0.0.1:8080").await?;
let addr = stream.local_addr()?;
source

pub fn peer_addr(&self) -> Result<SocketAddr>

Returns the remote address that this stream is connected to.

Examples
use async_std::net::TcpStream;

let stream = TcpStream::connect("127.0.0.1:8080").await?;
let peer = stream.peer_addr()?;
source

pub fn ttl(&self) -> Result<u32>

Gets the value of the IP_TTL option for this socket.

For more information about this option, see set_ttl.

Examples
use async_std::net::TcpStream;

let stream = TcpStream::connect("127.0.0.1:8080").await?;

stream.set_ttl(100)?;
assert_eq!(stream.ttl()?, 100);
source

pub 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_std::net::TcpStream;

let stream = TcpStream::connect("127.0.0.1:8080").await?;

stream.set_ttl(100)?;
assert_eq!(stream.ttl()?, 100);
source

pub async fn peek(&self, buf: &mut [u8]) -> Result<usize>

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 async_std::net::TcpStream;

let stream = TcpStream::connect("127.0.0.1:8000").await?;

let mut buf = vec![0; 1024];
let n = stream.peek(&mut buf).await?;
source

pub 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_std::net::TcpStream;

let stream = TcpStream::connect("127.0.0.1:8080").await?;

stream.set_nodelay(true)?;
assert_eq!(stream.nodelay()?, true);
source

pub fn set_nodelay(&self, nodelay: bool) -> Result<()>

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 async_std::net::TcpStream;

let stream = TcpStream::connect("127.0.0.1:8080").await?;

stream.set_nodelay(true)?;
assert_eq!(stream.nodelay()?, true);
source

pub fn shutdown(&self, how: Shutdown) -> Result<()>

Shuts down the read, write, or both halves of this connection.

This method will cause all pending and future I/O on the specified portions to return immediately with an appropriate value (see the documentation of Shutdown).

Examples
use std::net::Shutdown;

use async_std::net::TcpStream;

let stream = TcpStream::connect("127.0.0.1:8080").await?;
stream.shutdown(Shutdown::Both)?;

Trait Implementations§

source§

impl AsRawFd for TcpStream

source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
source§

impl AsyncRead for &TcpStream

source§

fn poll_read( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8] ) -> Poll<Result<usize>>

Attempt to read from the AsyncRead into buf. Read more
source§

fn poll_read_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &mut [IoSliceMut<'_>] ) -> Poll<Result<usize>>

Attempt to read from the AsyncRead into bufs using vectored IO operations. Read more
source§

impl AsyncRead for TcpStream

source§

fn poll_read( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8] ) -> Poll<Result<usize>>

Attempt to read from the AsyncRead into buf. Read more
source§

fn poll_read_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &mut [IoSliceMut<'_>] ) -> Poll<Result<usize>>

Attempt to read from the AsyncRead into bufs using vectored IO operations. Read more
source§

impl AsyncWrite for &TcpStream

source§

fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8] ) -> Poll<Result<usize>>

Attempt to write bytes from buf into the object. Read more
source§

fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>] ) -> Poll<Result<usize>>

Attempt to write bytes from bufs into the object using vectored IO operations. Read more
source§

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Attempt to flush the object, ensuring that any buffered data reach their destination. Read more
source§

fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Attempt to close the object. Read more
source§

impl AsyncWrite for TcpStream

source§

fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8] ) -> Poll<Result<usize>>

Attempt to write bytes from buf into the object. Read more
source§

fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>] ) -> Poll<Result<usize>>

Attempt to write bytes from bufs into the object using vectored IO operations. Read more
source§

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Attempt to flush the object, ensuring that any buffered data reach their destination. Read more
source§

fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Attempt to close the object. Read more
source§

impl Clone for TcpStream

source§

fn clone(&self) -> TcpStream

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for TcpStream

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<TcpStream> for TcpStream

source§

fn from(stream: TcpStream) -> TcpStream

Converts a std::net::TcpStream into its asynchronous equivalent.

source§

impl FromRawFd for TcpStream

source§

unsafe fn from_raw_fd(fd: RawFd) -> TcpStream

Constructs a new instance of Self from the given raw file descriptor. Read more
source§

impl IntoRawFd for TcpStream

source§

fn into_raw_fd(self) -> RawFd

Consumes this object, returning the raw underlying file descriptor. Read more
source§

impl TryFrom<TcpStream> for TcpStream

source§

fn try_from(stream: TcpStream) -> Result<TcpStream>

Converts a TcpStream into its synchronous equivalent.

§

type Error = Error

The type returned in the event of a conversion error.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<R> AsyncReadExt for R
where R: AsyncRead + ?Sized,

source§

fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>
where Self: Unpin,

Reads some bytes from the byte stream. Read more
source§

fn read_vectored<'a>( &'a mut self, bufs: &'a mut [IoSliceMut<'a>] ) -> ReadVectoredFuture<'a, Self>
where Self: Unpin,

Like read(), except it reads into a slice of buffers. Read more
source§

fn read_to_end<'a>( &'a mut self, buf: &'a mut Vec<u8> ) -> ReadToEndFuture<'a, Self>
where Self: Unpin,

Reads the entire contents and appends them to a Vec. Read more
source§

fn read_to_string<'a>( &'a mut self, buf: &'a mut String ) -> ReadToStringFuture<'a, Self>
where Self: Unpin,

Reads the entire contents and appends them to a String. Read more
source§

fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>
where Self: Unpin,

Reads the exact number of bytes required to fill buf. Read more
source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Converts this AsyncRead into a Stream of bytes. Read more
source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: AsyncRead, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
source§

fn boxed_reader<'a>(self) -> Pin<Box<dyn AsyncRead + Send + 'a>>
where Self: Sized + Send + 'a,

Boxes the reader and changes its type to dyn AsyncRead + Send + 'a. Read more
source§

impl<W> AsyncWriteExt for W
where W: AsyncWrite + ?Sized,

source§

fn write<'a>(&'a mut self, buf: &'a [u8]) -> WriteFuture<'a, Self>
where Self: Unpin,

Writes some bytes into the byte stream. Read more
source§

fn write_vectored<'a>( &'a mut self, bufs: &'a [IoSlice<'a>] ) -> WriteVectoredFuture<'a, Self>
where Self: Unpin,

Like write(), except that it writes a slice of buffers. Read more
source§

fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self>
where Self: Unpin,

Writes an entire buffer into the byte stream. Read more
source§

fn flush(&mut self) -> FlushFuture<'_, Self>
where Self: Unpin,

Flushes the stream to ensure that all buffered contents reach their destination. Read more
source§

fn close(&mut self) -> CloseFuture<'_, Self>
where Self: Unpin,

Closes the writer. Read more
source§

fn boxed_writer<'a>(self) -> Pin<Box<dyn AsyncWrite + Send + 'a>>
where Self: Sized + Send + 'a,

Boxes the writer and changes its type to dyn AsyncWrite + Send + 'a. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ReadExt for T
where T: AsyncRead + ?Sized,

source§

fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>
where Self: Unpin,

Reads some bytes from the byte stream. Read more
source§

fn read_vectored<'a>( &'a mut self, bufs: &'a mut [IoSliceMut<'a>] ) -> ReadVectoredFuture<'a, Self>
where Self: Unpin,

Like read, except that it reads into a slice of buffers. Read more
source§

fn read_to_end<'a>( &'a mut self, buf: &'a mut Vec<u8> ) -> ReadToEndFuture<'a, Self>
where Self: Unpin,

Reads all bytes from the byte stream. Read more
source§

fn read_to_string<'a>( &'a mut self, buf: &'a mut String ) -> ReadToStringFuture<'a, Self>
where Self: Unpin,

Reads all bytes from the byte stream and appends them into a string. Read more
source§

fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>
where Self: Unpin,

Reads the exact number of bytes required to fill buf. Read more
source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adaptor which will read at most limit bytes from it. Read more
source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adaptor for this instance of Read. Read more
source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Transforms this Read instance to a Stream over its bytes. Read more
source§

fn chain<R: Read>(self, next: R) -> Chain<Self, R>
where Self: Sized,

Creates an adaptor which will chain this stream with another. Read more
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WriteExt for T
where T: AsyncWrite + ?Sized,

source§

fn write<'a>(&'a mut self, buf: &'a [u8]) -> WriteFuture<'a, Self>
where Self: Unpin,

Writes some bytes into the byte stream. Read more
source§

fn flush(&mut self) -> FlushFuture<'_, Self>
where Self: Unpin,

Flushes the stream to ensure that all buffered contents reach their destination. Read more
source§

fn write_vectored<'a>( &'a mut self, bufs: &'a [IoSlice<'a>] ) -> WriteVectoredFuture<'a, Self>
where Self: Unpin,

Like write, except that it writes from a slice of buffers. Read more
source§

fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self>
where Self: Unpin,

Writes an entire buffer into the byte stream. Read more
source§

fn write_fmt<'a>(&'a mut self, fmt: Arguments<'_>) -> WriteFmtFuture<'a, Self>
where Self: Unpin,

Writes a formatted string into this writer, returning any error encountered. Read more