Add problem 01
This commit is contained in:
parent
79a9f030ac
commit
ce31df3a0c
3 changed files with 42 additions and 0 deletions
1
problem_01/.gitignore
vendored
Normal file
1
problem_01/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
/target/
|
||||||
9
problem_01/Cargo.toml
Normal file
9
problem_01/Cargo.toml
Normal 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
32
problem_01/src/main.rs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue