2个不稳定版本

0.6.0 2020年5月29日
0.5.0 2020年5月29日

#1066 in 异步

每月43次下载

MIT 协议

380KB
5.5K SLoC

futures-net

CI (Linux) Doc Version License


lib.rs:

异步网络TCP,UDP,UDS

这些类型的设计紧密遵循 std::net 中类似类型的API。

示例

TCP服务器

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客户端

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

依赖项

~1.5–2.3MB
~41K SLoC