[][src]Trait async_tcp::TcpStream

pub trait TcpStream: AsyncPeek + AsyncRead + AsyncWrite + Sized {
    fn connect(addr: SocketAddr) -> Connect<Self>

Notable traits for Connect<Stream>

impl<Stream: TcpStream> Future for Connect<Stream> type Output = Result<Stream>;
;
fn local_addr(&self) -> Result<SocketAddr>;
fn peer_addr(&self) -> Result<SocketAddr>;
fn shutdown(&self, how: Shutdown) -> Result<()>;
fn nodelay(&self) -> Result<bool>;
fn set_nodelay(&self, nodelay: bool) -> Result<()>;
fn ttl(&self) -> Result<u32>;
fn set_ttl(&self, ttl: u32) -> Result<()>; }

A TCP stream between a local and a remote socket.

A TcpStream can be created by either connecting to an endpoint or accepting a connection on a TcpListener.

TcpStream is a bidirectional stream that implements AsyncPeek, AsyncRead and AsyncWrite.

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.

Required methods

fn connect(addr: SocketAddr) -> Connect<Self>

Notable traits for Connect<Stream>

impl<Stream: TcpStream> Future for Connect<Stream> type Output = Result<Stream>;

Opens a TCP connection to the specified address.

Examples

use async_tcp::TcpStream;
use std::net::SocketAddr;
use std::str::FromStr;

// let addr = ...;
let stream = async_net::TcpStream::connect(addr).await?;
assert_eq!(addr, stream.peer_addr()?);

fn local_addr(&self) -> Result<SocketAddr>

Returns the socket address of the local half of this TCP connection.

Examples

use async_tcp::TcpStream;
use std::net::{IpAddr, SocketAddr};
use std::str::FromStr;

// let addr = ...;
let stream = async_net::TcpStream::connect(addr).await?;

assert_eq!(stream.local_addr()?.ip(), IpAddr::from_str("127.0.0.1").unwrap());

fn peer_addr(&self) -> Result<SocketAddr>

Returns the socket address of the remote peer of this TCP connection.

Examples

use async_tcp::TcpStream;
use std::net::SocketAddr;
use std::str::FromStr;

// let addr = ...;
let stream = async_net::TcpStream::connect(addr).await?;

assert_eq!(addr, stream.peer_addr()?);

fn shutdown(&self, how: Shutdown) -> Result<()>

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_tcp::TcpStream;
use std::net::{Shutdown, SocketAddr};
use std::str::FromStr;

// let addr = ...;
let stream = async_net::TcpStream::connect(addr).await?;

stream.shutdown(Shutdown::Both)?;

fn nodelay(&self) -> Result<bool>

Gets the value of the TCP_NODELAY option on this socket.

For more information about this option, see set_nodelay().

Examples

use async_tcp::TcpStream;
use std::net::SocketAddr;
use std::str::FromStr;

// let addr = ...;
let stream = async_net::TcpStream::connect(addr).await?;

stream.set_nodelay(true)?;
assert_eq!(stream.nodelay()?, true);

fn set_nodelay(&self, nodelay: bool) -> Result<()>

Sets the value of 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 reading of small packets.

Examples

use async_tcp::TcpStream;
use std::net::SocketAddr;
use std::str::FromStr;

// let addr = ...;
let stream = async_net::TcpStream::connect(addr).await?;

stream.set_nodelay(true)?;
assert_eq!(stream.nodelay()?, true);

fn ttl(&self) -> Result<u32>

Gets the value of the IP_TTL option on this socket.

For more information about this option, see set_ttl().

Examples

use async_tcp::TcpStream;
use std::net::SocketAddr;
use std::str::FromStr;

// let addr = ...;
let stream = async_net::TcpStream::connect(addr).await?;

stream.set_ttl(100)?;
assert_eq!(stream.ttl()?, 100);

fn set_ttl(&self, ttl: u32) -> Result<()>

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_tcp::TcpStream;
use std::net::SocketAddr;
use std::str::FromStr;

// let addr = ...;
let stream = async_net::TcpStream::connect(addr).await?;

stream.set_ttl(100)?;
assert_eq!(stream.ttl()?, 100);
Loading content...

Implementors

Loading content...