[][src]Struct async_std::net::TcpStream

pub struct TcpStream { /* fields omitted */ }

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 AsRawFd for TcpStream[src]

impl Clone for TcpStream[src]

impl Debug for TcpStream[src]

impl From<TcpStream> for TcpStream[src]

fn from(stream: TcpStream) -> TcpStream[src]

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

impl FromRawFd for TcpStream[src]

impl IntoRawFd for TcpStream[src]

impl Read for TcpStream[src]

impl<'_> Read for &'_ TcpStream[src]

impl Write for TcpStream[src]

impl<'_> Write for &'_ TcpStream[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.