tcpclient

Crates.iotcpclient
lib.rstcpclient
version2.0.0
sourcesrc
created_at2020-11-27 07:38:14.616594
updated_at2024-01-18 05:30:13.982433
descriptionAsynchronous tcpclient based on aqueue actor.
homepage
repositoryhttps://github.com/luyikk/tcpclient
max_upload_size
id316961
size9,075
(luyikk)

documentation

https://docs.rs/tcpclient

README

Asynchronous tcpclient based on aqueue actor.

  • Version 2.+ Minimum supported Rust version: 1.75 or later

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]).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").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(())
}
Commit count: 33

cargo fmt