Compare commits

...

2 Commits

Author SHA1 Message Date
7ae9903d95
Reading bytes time 2021-06-01 10:27:17 -04:00
8f3897e097
Fun with tcp 2021-06-01 08:49:27 -04:00
3 changed files with 51 additions and 0 deletions

5
httpong/Cargo.lock generated Normal file
View File

@ -0,0 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "httpong"
version = "0.1.0"

9
httpong/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "httpong"
version = "0.1.0"
authors = ["Dan Buch <dan@meatballhat.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

37
httpong/src/main.rs Normal file
View File

@ -0,0 +1,37 @@
use std::env;
use std::io::Read;
use std::net::{TcpListener, TcpStream};
fn handle_client(stream: &mut TcpStream) -> std::io::Result<()> {
println!("---> handling stream {:?}", stream);
let mut buf = [0; 1024];
let n = stream.read(&mut buf[..])?;
let req = std::str::from_utf8(&buf[..n]);
match req {
Ok(s) => {
println!("---> first {} bytes: {:?}", n, s);
Ok(())
}
Err(e) => {
println!("---> oh no {:?}", e);
Ok(())
}
}
}
fn main() -> std::io::Result<()> {
let mut addr = String::from("127.0.0.1:19721");
if let Ok(v) = env::var("ADDR") {
addr = String::from(v.as_str());
}
println!("listening at {}", addr);
let listener = TcpListener::bind(addr)?;
for stream in listener.incoming() {
if let Ok(_) = handle_client(&mut stream?) {
println!("---> looks like that went well")
}
}
Ok(())
}