#tcp #channel #client #testing #tcp-client #async-await #wait #connect #read #send

tcp-channel-client

Asynchronous tcpclient based on aqueue actor

2 unstable releases

0.2.0 Jan 18, 2024
0.1.0 Jul 15, 2023

#1051 in Asynchronous

Download history 24/week @ 2023-10-29 17/week @ 2023-11-05 27/week @ 2023-11-12 26/week @ 2023-11-19 12/week @ 2023-11-26 14/week @ 2023-12-03 8/week @ 2023-12-10 22/week @ 2023-12-17 15/week @ 2023-12-24 28/week @ 2023-12-31 25/week @ 2024-01-07 87/week @ 2024-01-14 16/week @ 2024-01-21 17/week @ 2024-01-28 9/week @ 2024-02-04 18/week @ 2024-02-11

60 downloads per month
Used in 2 crates (via netxclient)

MIT/Apache

9KB
159 lines

Asynchronous tcpclient based on aqueue actor.

DEMO URL:

demo url

examples echo:

#![feature(async_closure)]
use tcpclient::{TcpClient,SocketClientTrait};
use tokio::io::AsyncReadExt;
use std::error::Error;
use log::LevelFilter;

#[tokio::main]
async fn main()->Result<(),Box<dyn Error>> {
    // set logger out
    env_logger::Builder::new().filter_level(LevelFilter::Debug).init();

    // connect echo server
    let client =
        TcpClient::connect("127.0.0.1:5555", async move |_, client, mut reader| {
            // read buff from target server
            let mut buff=[0;7];
            while let Ok(len) = reader.read_exact(&mut buff).await {
                // send buff to target server
                println!("{}",std::str::from_utf8(&buff[..len])?);
                client.send(buff[..len].to_vec()).await?;
            }
            // return true need disconnect,false not disconnect
            // if true and the current state is disconnected, it will be ignored.
            Ok(true)
        }, ()).await?;

    // connect ok send buff to target server
    client.send(b"1234567".to_vec()).await?;

    // test disconnect readline 
    let mut str = "".into();
    std::io::stdin().read_line(&mut str)?;

    // disconnect target server
    client.disconnect().await?;
    // wait env logger out show
    std::io::stdin().read_line(&mut str)?;
    Ok(())
}

Dependencies

~3–13MB
~116K SLoC