diff --git a/problem_01/.gitignore b/problem_01/.gitignore new file mode 100644 index 0000000..b83d222 --- /dev/null +++ b/problem_01/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/problem_01/Cargo.toml b/problem_01/Cargo.toml new file mode 100644 index 0000000..20b0f47 --- /dev/null +++ b/problem_01/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "problem_01" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +tokio = { version = "1.27.0", features = ["full"] } diff --git a/problem_01/src/main.rs b/problem_01/src/main.rs new file mode 100644 index 0000000..3b3e7e5 --- /dev/null +++ b/problem_01/src/main.rs @@ -0,0 +1,32 @@ +use tokio::net::TcpListener; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let listener = TcpListener::bind("127.0.0.1:4040").await?; + + loop { + let (mut socket, _) = listener.accept().await?; + + tokio::spawn(async move { + let mut buf = [0; 1024]; + + loop { + + let n = match socket.read(&mut buf).await { + Ok(n) if n == 0 => return, + Ok(n) => n, + Err(e) => { + eprintln!("failed to read from socket; err = {:?}", e); + return; + } + }; + + if let Err(e) = socket.write_all(&buf[0..n]).await { + eprintln!("failed to write to socket; err = {:?}", e); + return; + } + } + }); + } +}