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

53
__network/socket2/Cargo.lock generated Normal file
View File

@@ -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"

View File

@@ -0,0 +1,11 @@
[package]
name = "socket2"
version = "0.1.0"
authors = ["Hatter Jiang <jht5945@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
socket2 = "0.3.19"

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));
}
}