Struct async_std::net::TcpStream [−][src]
pub struct TcpStream { /* fields omitted */ }
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
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?;
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()?;
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()?;
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);
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?;
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);
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);
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
Extracts the underlying raw socket from this object.
Constructs a new instance of Self
from the given raw file
descriptor. Read more
Creates a new I/O object from the given raw socket. Read more
Consumes this object, returning the raw underlying file descriptor. Read more
Consumes this object, returning the raw underlying socket. Read more
Attempt to read from the AsyncRead
into buf
.
Attempt to read from the AsyncRead
into bufs
using vectored IO operations.
Reads some bytes from the byte stream. Read more
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ImplFuture<Result<usize>> where
Self: Unpin,
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ImplFuture<Result<usize>> where
Self: Unpin,
Reads all bytes from the byte stream. Read more
Reads all bytes from the byte stream and appends them into a string. Read more
Reads the exact number of bytes required to fill buf
. Read more
Creates an adaptor which will read at most limit
bytes from it. Read more
Creates a “by reference” adaptor for this instance of Read
. Read more
Transforms this Read
instance to a Stream
over its bytes. Read more
Attempt to read from the AsyncRead
into buf
.
Attempt to read from the AsyncRead
into bufs
using vectored IO operations.
Reads some bytes from the byte stream. Read more
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ImplFuture<Result<usize>> where
Self: Unpin,
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ImplFuture<Result<usize>> where
Self: Unpin,
Reads all bytes from the byte stream. Read more
Reads all bytes from the byte stream and appends them into a string. Read more
Reads the exact number of bytes required to fill buf
. Read more
Creates an adaptor which will read at most limit
bytes from it. Read more
Creates a “by reference” adaptor for this instance of Read
. Read more
Transforms this Read
instance to a Stream
over its bytes. Read more
Attempt to write bytes from buf
into the object.
Attempt to write bytes from bufs
into the object using vectored IO operations.
Attempt to flush the object, ensuring that any buffered data reach their destination. Read more
Attempt to close the object.
Writes some bytes into the byte stream. Read more
Flushes the stream to ensure that all buffered contents reach their destination. Read more
Writes an entire buffer into the byte stream. Read more
Attempt to write bytes from buf
into the object.
Attempt to write bytes from bufs
into the object using vectored IO operations.
Attempt to flush the object, ensuring that any buffered data reach their destination. Read more
Attempt to close the object.
Writes some bytes into the byte stream. Read more
Flushes the stream to ensure that all buffered contents reach their destination. Read more
Writes an entire buffer into the byte stream. Read more
Auto Trait Implementations
impl RefUnwindSafe for TcpStream
impl UnwindSafe for TcpStream
Blanket Implementations
Mutably borrows from an owned value. Read more