Skip to content

Instantly share code, notes, and snippets.

@ahndmal
Created July 7, 2023 17:29
Show Gist options
  • Save ahndmal/2391fe312873725b879a25076afea5b2 to your computer and use it in GitHub Desktop.
Save ahndmal/2391fe312873725b879a25076afea5b2 to your computer and use it in GitHub Desktop.
use std::io::{Read, Write};
use std::{net, time};
use std::future::Future;
fn main() {
let port = 8084;
let server = net::TcpListener::bind(format!("127.0.0.1:{port}")).unwrap();
println!("[ TCP ] Server started on {} with port {}",
server.local_addr().unwrap().ip(), port);
let headers = "Content-Type: text/html";
loop {
match server.accept() {
Ok((mut sock, addr)) => {
println!("new client {addr:?}");
let data = "<h2>Hello Rust</h2>";
let resp = format!("{headers}\n\n{data}");
let response = "HTTP/1.1 200 OK\nContent-Type: text/html\n\nTEST".as_bytes();
println!("{:?}", std::str::from_utf8(&response).unwrap());
sock.write_all(&response).expect("Could not write to client 😎");
},
Err(e) => println!("no client {e:?}"),
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment