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

A TCP connection.

A TcpStream can be created by connecting to an endpoint or by accepting 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::prelude::*;

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

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?;

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()?);

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()?);

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)?;

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?;

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()?);

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)?;

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()?);

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

Extracts the raw file descriptor. Read more

Attempt to read from the AsyncRead into buf. Read more

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

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

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

Attempt to close the object. Read more

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Reads some bytes from the byte stream. Read more

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

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

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

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

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

Converts this AsyncRead into a Stream of bytes. Read more

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

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

Writes some bytes into the byte stream. Read more

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

Writes an entire buffer into the byte stream. Read more

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

Closes the writer. Read more

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

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.