[−][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?;
Implementations
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 AsRawFd for TcpStream
[src]
impl AsRawSocket for TcpStream
[src]
pub fn as_raw_socket(&self) -> RawSocket
[src]
impl Clone for TcpStream
[src]
impl Debug for TcpStream
[src]
impl From<TcpStream> for TcpStream
[src]
pub fn from(stream: TcpStream) -> TcpStream
[src]
Converts a std::net::TcpStream
into its asynchronous equivalent.
impl FromRawFd for TcpStream
[src]
pub unsafe fn from_raw_fd(fd: RawFd) -> TcpStream
[src]
impl FromRawSocket for TcpStream
[src]
pub unsafe fn from_raw_socket(handle: RawSocket) -> TcpStream
[src]
impl IntoRawFd for TcpStream
[src]
pub fn into_raw_fd(self) -> RawFd
[src]
impl IntoRawSocket for TcpStream
[src]
pub fn into_raw_socket(self) -> RawSocket
[src]
impl Read for TcpStream
[src]
pub 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>>
pub 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>>
pub fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ImplFuture<Result<usize>> where
Self: Unpin,
[src]
Self: Unpin,
pub 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,
pub 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,
pub 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,
pub fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ImplFuture<Result<()>> where
Self: Unpin,
[src]
Self: Unpin,
pub fn take(self, limit: u64) -> Take<Self> where
Self: Sized,
[src]
Self: Sized,
pub fn by_ref(&mut self) -> &mut Self where
Self: Sized,
[src]
Self: Sized,
pub fn bytes(self) -> Bytes<Self> where
Self: Sized,
[src]
Self: Sized,
pub fn chain<R: Read>(self, next: R) -> Chain<Self, R> where
Self: Sized,
[src]
Self: Sized,
impl Read for &TcpStream
[src]
pub 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>>
pub 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>>
pub fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ImplFuture<Result<usize>> where
Self: Unpin,
[src]
Self: Unpin,
pub 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,
pub 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,
pub 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,
pub fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ImplFuture<Result<()>> where
Self: Unpin,
[src]
Self: Unpin,
pub fn take(self, limit: u64) -> Take<Self> where
Self: Sized,
[src]
Self: Sized,
pub fn by_ref(&mut self) -> &mut Self where
Self: Sized,
[src]
Self: Sized,
pub fn bytes(self) -> Bytes<Self> where
Self: Sized,
[src]
Self: Sized,
pub fn chain<R: Read>(self, next: R) -> Chain<Self, R> where
Self: Sized,
[src]
Self: Sized,
impl Write for TcpStream
[src]
pub 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>>
pub 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>>
pub fn poll_flush(
self: Pin<&mut Self>,
cx: &mut Context<'_>
) -> Poll<Result<()>>
[src]
self: Pin<&mut Self>,
cx: &mut Context<'_>
) -> Poll<Result<()>>
pub fn poll_close(
self: Pin<&mut Self>,
cx: &mut Context<'_>
) -> Poll<Result<()>>
[src]
self: Pin<&mut Self>,
cx: &mut Context<'_>
) -> Poll<Result<()>>
pub fn write<'a>(&'a mut self, buf: &'a [u8]) -> ImplFuture<Result<usize>> where
Self: Unpin,
[src]
Self: Unpin,
pub fn flush(&mut self) -> ImplFuture<Result<()>> where
Self: Unpin,
[src]
Self: Unpin,
pub 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,
pub fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> ImplFuture<Result<()>> where
Self: Unpin,
[src]
Self: Unpin,
pub fn write_fmt<'a>(&'a mut self, fmt: Arguments<'_>) -> ImplFuture<Result<()>> where
Self: Unpin,
[src]
Self: Unpin,
impl Write for &TcpStream
[src]
pub 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>>
pub fn poll_flush(
self: Pin<&mut Self>,
cx: &mut Context<'_>
) -> Poll<Result<()>>
[src]
self: Pin<&mut Self>,
cx: &mut Context<'_>
) -> Poll<Result<()>>
pub fn poll_close(
self: Pin<&mut Self>,
cx: &mut Context<'_>
) -> Poll<Result<()>>
[src]
self: Pin<&mut Self>,
cx: &mut Context<'_>
) -> Poll<Result<()>>
pub 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>>
pub fn write<'a>(&'a mut self, buf: &'a [u8]) -> ImplFuture<Result<usize>> where
Self: Unpin,
[src]
Self: Unpin,
pub fn flush(&mut self) -> ImplFuture<Result<()>> where
Self: Unpin,
[src]
Self: Unpin,
pub 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,
pub fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> ImplFuture<Result<()>> where
Self: Unpin,
[src]
Self: Unpin,
pub fn write_fmt<'a>(&'a mut self, fmt: Arguments<'_>) -> ImplFuture<Result<()>> where
Self: Unpin,
[src]
Self: Unpin,
Auto Trait Implementations
impl RefUnwindSafe for TcpStream
[src]
impl Send for TcpStream
[src]
impl Sync for TcpStream
[src]
impl Unpin for TcpStream
[src]
impl UnwindSafe for TcpStream
[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub 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.
pub fn to_owned(&self) -> T
[src]
pub 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.
pub 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>,