Crate futures_net

Source
Expand description

§Async network TCP, UDP, UDS

The types are designed to closely follow the APIs of the analogous types in std::net in Asychronous versions.

§Examples

TCP Server

use futures_net::{TcpListener, TcpStream, runtime::Runtime};
use futures::prelude::*;

async fn say_hello(mut stream: TcpStream) {
    stream.write_all(b"Shall I hear more, or shall I speak at this?").await;
}

#[futures_net::main]
async fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {
    let socket_addr = "127.0.0.1:8080".parse()?;
    let mut listener = TcpListener::bind(&socket_addr)?;
    let mut incoming = listener.incoming();

    // accept connections and process them serially
    while let Some(stream) = incoming.next().await {
        say_hello(stream?).await;
    }
    Ok(())
}

TCP Client

use std::error::Error;
use futures::prelude::*;
use futures_net::{TcpListener, TcpStream, runtime::Runtime};

#[futures_net::main]
async fn main() -> Result<(), Box<dyn Error + 'static>> {
    let socket_addr = "127.0.0.1:8080".parse()?;
    let mut buffer = vec![];
    let mut stream = TcpStream::connect(&socket_addr).await?;

    stream.read(&mut buffer).await?;
    println!("{:?}", buffer);
    Ok(())
}

Modules§

driver
futures reactor, event loop.
runtime
Futures Async Execute Engine
tcp
Async TCP.
udp
A UDP socket.
uds
Async UDS (Unix Domain Sockets)

Structs§

TcpListener
A TCP socket server, listening for connections.
TcpStream
A TCP stream between a local and a remote socket.
UdpSocket
A UDP socket.
UnixDatagram
An I/O object representing a Unix datagram socket.
UnixListener
A Unix socket cna accept connections from other Unix sockets.
UnixStream
A structure representing a connected Unix socket.

Attribute Macros§

main
Marks async function for futures_net.
test
Marks async test function for futures_net.