diff --git a/__network/socket2/Cargo.lock b/__network/socket2/Cargo.lock new file mode 100644 index 0000000..19f922a --- /dev/null +++ b/__network/socket2/Cargo.lock @@ -0,0 +1,53 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "libc" +version = "0.2.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb" + +[[package]] +name = "socket2" +version = "0.1.0" +dependencies = [ + "socket2 0.3.19", +] + +[[package]] +name = "socket2" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "122e570113d28d773067fab24266b66753f6ea915758651696b6e35e49f88d6e" +dependencies = [ + "cfg-if", + "libc", + "winapi", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/__network/socket2/Cargo.toml b/__network/socket2/Cargo.toml new file mode 100644 index 0000000..f8953a1 --- /dev/null +++ b/__network/socket2/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "socket2" +version = "0.1.0" +authors = ["Hatter Jiang "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +socket2 = "0.3.19" + diff --git a/__network/socket2/src/main.rs b/__network/socket2/src/main.rs new file mode 100644 index 0000000..a1cc52f --- /dev/null +++ b/__network/socket2/src/main.rs @@ -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::().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)); + } +}