socker start
Cargo Check, Format, Fix and Test / cargo CI (push) Successful in 2m27s Details

This commit is contained in:
Christoph J. Scherr 2024-01-15 17:27:50 +01:00
parent 3c7a4297f5
commit 25baf7cd2f
Signed by: cscherrNT
GPG Key ID: 8E2B45BC51A27EA7
4 changed files with 54 additions and 0 deletions

4
Cargo.lock generated
View File

@ -1214,6 +1214,10 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7c388c1b5e93756d0c740965c41e8822f866621d41acbdf6336a6a168f8840c"
[[package]]
name = "socker"
version = "0.1.0"
[[package]]
name = "strsim"
version = "0.10.0"

View File

@ -7,6 +7,7 @@ members = [
"members/revsqrt",
"members/serde-json-demo",
"members/slog-demo",
"members/socker",
]
default-members = [
".",
@ -16,6 +17,7 @@ default-members = [
"members/slog-demo",
"members/cucumber-demo",
"members/panic-calm",
"members/socker",
]
[package]

View File

@ -0,0 +1,8 @@
[package]
name = "socker"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -0,0 +1,40 @@
use std::{
io::{BufRead, BufReader, Write},
net::{TcpListener, TcpStream},
process::exit,
};
fn main() {
println!("Starting the server");
let target_address = "127.0.0.1:7878";
let listener = match TcpListener::bind(target_address) {
Ok(listener) => listener,
Err(err) => {
eprintln!("Could not start server: {err}");
exit(1);
}
};
for stream in listener.incoming() {
let stream = stream.unwrap();
handle_connection(stream);
}
}
fn handle_connection(mut stream: TcpStream) {
let buf_rd = BufReader::new(&mut stream);
let request: Vec<_> = buf_rd
.lines()
.map(|result| result.unwrap())
.take_while(|line| !line.is_empty())
.collect();
println!("Request: {:#?}", request);
match request[0].as_str() {
"ping" => {
writeln!(stream, "> pong!").unwrap();
}
_ => {
writeln!(stream, "> command not recognized.").unwrap();
}
}
}