Created
July 7, 2023 17:29
-
-
Save ahndmal/2391fe312873725b879a25076afea5b2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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