Added Basic network server

This commit is contained in:
Sebastian 2024-05-05 14:42:25 +02:00
parent 584eb4aff0
commit 6bf5b6aa1d
3 changed files with 475 additions and 2 deletions

View file

@ -1,5 +1,49 @@
mod rotctl;
fn main() {
println!("Hello, world!");
use tokio::{
self,
io::{AsyncBufReadExt, AsyncWriteExt, BufStream},
net::{TcpListener, TcpStream},
};
use std::io;
async fn process_socket(socket: TcpStream) {
let mut stream = BufStream::new(socket);
let mut line = String::new();
loop {
if let Ok(n) = stream.read_line(&mut line).await {
if n == 0 {
return;
}
println!("> {}", line);
match rotctl::parse_command(&line) {
Ok(cmd) => println!("Commmand: {:?}", cmd),
Err(msg) => {
stream.write_all(msg.as_bytes()).await.unwrap();
stream.flush().await.unwrap();
}
}
line.clear();
} else {
return;
}
}
}
#[tokio::main]
async fn main() -> io::Result<()> {
let listener = TcpListener::bind("127.0.0.1:8080").await?;
loop {
let (socket, _) = listener.accept().await?;
tokio::spawn(async move {
process_socket(socket).await;
});
}
}