feat: add socket2

This commit is contained in:
2020-12-26 09:39:37 +08:00
parent e3ac142ead
commit 88c4ba5c94
3 changed files with 90 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
use std::thread;
use std::time::Duration;
use std::net::SocketAddr;
use socket2::{Socket, Domain, Type};
fn main() {
loop {
// create a TCP listener bound to two addresses
let socket = Socket::new(Domain::ipv6(), Type::stream(), None).unwrap();
socket.bind(&"[::1]:12345".parse::<SocketAddr>().unwrap().into()).unwrap();
// socket.set_only_v6(false).unwrap();
socket.listen(128).unwrap();
let listener = socket.into_tcp_listener();
println!("Listening ...");
let (tcp_stream, socket_address) = listener.accept().unwrap();
println!("{:?}", socket_address);
println!("{:?}", tcp_stream);
drop(listener);
println!("Sleeping ...");
thread::sleep(Duration::from_secs(10));
}
}