Add problem 01

This commit is contained in:
Bastian Gruber 2023-04-17 21:45:27 +02:00
parent 79a9f030ac
commit ce31df3a0c
No known key found for this signature in database
GPG key ID: BE9F8C772B188CBF
3 changed files with 42 additions and 0 deletions

1
problem_01/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target/

9
problem_01/Cargo.toml Normal file
View file

@ -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"] }

32
problem_01/src/main.rs Normal file
View file

@ -0,0 +1,32 @@
use tokio::net::TcpListener;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
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;
}
}
});
}
}