Compare commits
2 Commits
05043d778a
...
7ae9903d95
Author | SHA1 | Date | |
---|---|---|---|
7ae9903d95 | |||
8f3897e097 |
5
httpong/Cargo.lock
generated
Normal file
5
httpong/Cargo.lock
generated
Normal 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
9
httpong/Cargo.toml
Normal 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
37
httpong/src/main.rs
Normal 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(())
|
||||
}
|
Loading…
Reference in New Issue
Block a user