foxtrot/examples/async-echo-server.rs

43 lines
1 KiB
Rust

use async_join::join_all;
use foxtrot::Async;
async fn echo(port: u16) -> Result<(), foxtrot::Error> {
let listener = Async::bind([127, 0, 0, 1].into(), port).await?;
println!("bound listener");
loop {
let (stream, _addr) = listener.accept().await?;
println!("Accepted connection");
loop {
let mut buf = [0; 1024];
if let Err(e) = stream.read(&mut buf).await {
if e == rustix::io::Error::PIPE {
break;
}
return Err(e.into());
}
if let Err(e) = stream.write_all(&buf).await {
if e == rustix::io::Error::PIPE {
break;
}
return Err(e.into());
}
}
println!("Connection closed");
}
}
fn main() -> Result<(), foxtrot::Error> {
foxtrot::block_on(async move {
for res in join_all(vec![Box::pin(echo(4444)), Box::pin(echo(4445))]).await {
res?;
}
Ok(())
})?
}