Created
January 15, 2020 21:48
-
-
Save tomasol/8df4e00892b80f8b1452f61e102480c6 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::thread; | |
use std::net::SocketAddr; | |
use socket2::{Socket, Domain, Type}; | |
use std::net::Shutdown; | |
use std::sync::Arc; | |
fn main() { | |
let socket = Arc::new(Socket::new(Domain::ipv4(), Type::stream(), None).unwrap()); | |
socket.bind(&"127.0.0.1:8080".parse::<SocketAddr>().unwrap().into()).unwrap(); | |
socket.listen(128).unwrap(); | |
let socket2 = socket.clone(); | |
let handler = thread::spawn(move || { | |
println!("accepting"); | |
match socket2.accept() { | |
Ok(_) => println!("new client"), | |
Err(e) => println!("got error: {:?}", e), | |
} | |
}); | |
std::thread::sleep(std::time::Duration::from_secs(1)); | |
socket.shutdown(Shutdown::Both).unwrap(); | |
handler.join().unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment