use std::{net::Ipv4Addr, str::FromStr};
use async_dns_lookup::AsyncDnsResolver;
use tokio::runtime::Runtime;
fn main() {
let rt = Runtime::new().unwrap();
rt.block_on(async {
let dns = AsyncDnsResolver::new();
let ht = tokio::spawn(async move {
let times = 20;
for i in 1..=times {
println!("Tick {i}/{times}");
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}
println!("background task done");
});
let dns1 = dns.clone();
let ht1 = tokio::spawn(async move {
let hostname = dns1
.reverse_lookup(Ipv4Addr::from_str("192.168.100.1").unwrap())
.await
.unwrap();
println!("Hostname 1: {hostname}");
});
let dns2 = dns.clone();
let ht2 = tokio::spawn(async move {
let hostname = dns2
.reverse_lookup(Ipv4Addr::from_str("192.168.100.2").unwrap())
.await
.unwrap();
println!("Hostname 2: {hostname}");
});
let dns3 = dns.clone();
let ht3 = tokio::spawn(async move {
let hostname = dns3
.reverse_lookup(Ipv4Addr::from_str("192.168.100.3").unwrap())
.await
.unwrap();
println!("Hostname 3: {hostname}");
});
let _ = tokio::join!(ht, ht1, ht2, ht3);
println!("All thread are done");
});
}