Fun with tcp

This commit is contained in:
Dan Buch 2021-06-01 08:49:27 -04:00
parent 05043d778a
commit 8f3897e097
Signed by: meatballhat
GPG Key ID: 9685130D8B763EA7
3 changed files with 36 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]

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

@ -0,0 +1,22 @@
use std::env;
use std::net::{TcpListener, TcpStream};
fn handle_client(stream: TcpStream) {
println!("---> handling stream {:?}", stream);
// ...
}
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() {
handle_client(stream?);
}
Ok(())
}