Created
February 11, 2023 11:55
-
-
Save kunugibaru/cb257037246b79183e251b3fd8720d13 to your computer and use it in GitHub Desktop.
This file contains 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::TcpListener; | |
use std::thread; | |
fn main() { | |
let listener = TcpListener::bind("localhost:8080").unwrap(); | |
for stream in listener.incoming() { | |
let stream = stream.unwrap(); | |
println!("Received connection from {:?}", stream.peer_addr().unwrap()); | |
thread::spawn(|| { | |
handle_connection(stream); | |
}); | |
} | |
} | |
fn handle_connection(mut stream: std::net::TcpStream) { | |
let mut buffer = [0; 1024]; | |
stream.read(&mut buffer).unwrap(); | |
let response = "HTTP/1.1 200 OK\r\n\r\nHello, World!"; | |
stream.write(response.as_bytes()).unwrap(); | |
stream.flush().unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment