[−][src]Struct async_std::net::TcpStream
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?;
Methods
impl TcpStream
[src]
pub async fn connect<A: ToSocketAddrs>(addrs: A) -> Result<TcpStream>
[src]
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?;
pub fn local_addr(&self) -> Result<SocketAddr>
[src]
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()?;
pub fn peer_addr(&self) -> Result<SocketAddr>
[src]
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()?;
pub fn ttl(&self) -> Result<u32>
[src]
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);
pub fn set_ttl(&self, ttl: u32) -> Result<()>
[src]
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);
pub async fn peek<'_, '_>(&'_ self, buf: &'_ mut [u8]) -> Result<usize>
[src]
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?;
pub fn nodelay(&self) -> Result<bool>
[src]
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);
pub fn set_nodelay(&self, nodelay: bool) -> Result<()>
[src]
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);
pub fn shutdown(&self, how: Shutdown) -> Result<()>
[src]
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
impl Read for TcpStream
[src]
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8]
) -> Poll<Result<usize>>
fn poll_read_vectored(
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &mut [IoSliceMut]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &mut [IoSliceMut]
) -> Poll<Result<usize>>
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ImplFuture<Result<usize>> where
Self: Unpin,
[src]
Self: Unpin,
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ImplFuture<Result<usize>> where
Self: Unpin,
[src]
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ImplFuture<Result<usize>> where
Self: Unpin,
fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>
) -> ImplFuture<Result<usize>> where
Self: Unpin,
[src]
&'a mut self,
buf: &'a mut Vec<u8>
) -> ImplFuture<Result<usize>> where
Self: Unpin,
fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String
) -> ImplFuture<Result<usize>> where
Self: Unpin,
[src]
&'a mut self,
buf: &'a mut String
) -> ImplFuture<Result<usize>> where
Self: Unpin,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ImplFuture<Result<()>> where
Self: Unpin,
[src]
Self: Unpin,
fn take(self, limit: u64) -> Take<Self> where
Self: Sized,
[src]
Self: Sized,
fn by_ref(&mut self) -> &mut Self where
Self: Sized,
[src]
Self: Sized,
fn bytes(self) -> Bytes<Self> where
Self: Sized,
[src]
Self: Sized,
fn chain<R: Read>(self, next: R) -> Chain<Self, R> where
Self: Sized,
[src]
Self: Sized,
impl<'_> Read for &'_ TcpStream
[src]
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8]
) -> Poll<Result<usize>>
fn poll_read_vectored(
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &mut [IoSliceMut]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &mut [IoSliceMut]
) -> Poll<Result<usize>>
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ImplFuture<Result<usize>> where
Self: Unpin,
[src]
Self: Unpin,
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ImplFuture<Result<usize>> where
Self: Unpin,
[src]
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ImplFuture<Result<usize>> where
Self: Unpin,
fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>
) -> ImplFuture<Result<usize>> where
Self: Unpin,
[src]
&'a mut self,
buf: &'a mut Vec<u8>
) -> ImplFuture<Result<usize>> where
Self: Unpin,
fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String
) -> ImplFuture<Result<usize>> where
Self: Unpin,
[src]
&'a mut self,
buf: &'a mut String
) -> ImplFuture<Result<usize>> where
Self: Unpin,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ImplFuture<Result<()>> where
Self: Unpin,
[src]
Self: Unpin,
fn take(self, limit: u64) -> Take<Self> where
Self: Sized,
[src]
Self: Sized,
fn by_ref(&mut self) -> &mut Self where
Self: Sized,
[src]
Self: Sized,
fn bytes(self) -> Bytes<Self> where
Self: Sized,
[src]
Self: Sized,
fn chain<R: Read>(self, next: R) -> Chain<Self, R> where
Self: Sized,
[src]
Self: Sized,
impl Write for TcpStream
[src]
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &[u8]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
buf: &[u8]
) -> Poll<Result<usize>>
fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &[IoSlice]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &[IoSlice]
) -> Poll<Result<usize>>
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<()>>
[src]
fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<()>>
[src]
fn write<'a>(&'a mut self, buf: &'a [u8]) -> ImplFuture<Result<usize>> where
Self: Unpin,
[src]
Self: Unpin,
fn flush(&mut self) -> ImplFuture<Result<()>> where
Self: Unpin,
[src]
Self: Unpin,
fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> ImplFuture<Result<usize>> where
Self: Unpin,
[src]
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> ImplFuture<Result<usize>> where
Self: Unpin,
fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> ImplFuture<Result<()>> where
Self: Unpin,
[src]
Self: Unpin,
fn write_fmt<'a>(&'a mut self, fmt: Arguments) -> ImplFuture<Result<()>> where
Self: Unpin,
[src]
Self: Unpin,
impl<'_> Write for &'_ TcpStream
[src]
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &[u8]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
buf: &[u8]
) -> Poll<Result<usize>>
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<()>>
[src]
fn poll_close(self: Pin<&mut Self>, _: &mut Context) -> Poll<Result<()>>
[src]
fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &[IoSlice]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &[IoSlice]
) -> Poll<Result<usize>>
fn write<'a>(&'a mut self, buf: &'a [u8]) -> ImplFuture<Result<usize>> where
Self: Unpin,
[src]
Self: Unpin,
fn flush(&mut self) -> ImplFuture<Result<()>> where
Self: Unpin,
[src]
Self: Unpin,
fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> ImplFuture<Result<usize>> where
Self: Unpin,
[src]
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> ImplFuture<Result<usize>> where
Self: Unpin,
fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> ImplFuture<Result<()>> where
Self: Unpin,
[src]
Self: Unpin,
fn write_fmt<'a>(&'a mut self, fmt: Arguments) -> ImplFuture<Result<()>> where
Self: Unpin,
[src]
Self: Unpin,
impl AsRawFd for TcpStream
[src]
impl FromRawFd for TcpStream
[src]
unsafe fn from_raw_fd(fd: RawFd) -> TcpStream
[src]
impl IntoRawFd for TcpStream
[src]
fn into_raw_fd(self) -> RawFd
[src]
impl From<TcpStream> for TcpStream
[src]
fn from(stream: TcpStream) -> TcpStream
[src]
Converts a std::net::TcpStream
into its asynchronous equivalent.
impl Debug for TcpStream
[src]
Auto Trait Implementations
impl Send for TcpStream
impl Sync for TcpStream
impl Unpin for TcpStream
impl UnwindSafe for TcpStream
impl RefUnwindSafe for TcpStream
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for 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>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
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> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,