2 unstable releases

0.6.0 May 29, 2020
0.5.0 May 29, 2020

#957 in Asynchronous

Download history 15/week @ 2024-07-28 29/week @ 2024-08-04 18/week @ 2024-08-11 4/week @ 2024-08-25 17/week @ 2024-09-01 49/week @ 2024-09-08 23/week @ 2024-09-22 1/week @ 2024-09-29 37/week @ 2024-10-06 20/week @ 2024-10-13 23/week @ 2024-10-20 14/week @ 2024-10-27 10/week @ 2024-11-03 23/week @ 2024-11-10

71 downloads per month

MIT license

380KB
5.5K SLoC

futures-net

CI (Linux) Doc Version License


lib.rs:

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(())
}

Dependencies

~1.5–2.3MB
~41K SLoC