I am trying to write a simple network scanner to find hosts with a specific open port using Tokio::net::TcpStream::connect. Open connections are really fast, but a closed/blocked port results in a delay of 3-5 seconds. So scanning 254 ports takes about 130 seconds on my pc multi-threaded. I can't find the reason, I was hoping someone here might be able to lend some insight into the reason and maybe a solution to speed it up.
You likely are right. I'm obviously still learning and trying to figure things out. This is the code that I have working. I know it's ugly and I'm wide open to criticisms and suggestions. I'm still new and trying to learn idiomatic rust.
println!("starting");
let mut stream_vec = Vec::new();
for i in 1..254{
let ip = "10.0.3.";
let jh = tokio::spawn(
async move{
let f = format!("{}{}:{}",ip.clone(),i,"80");
let ts = TcpStream::connect(f.clone()).await;
match ts{
Ok(a)=>{
println!("{:?}",&a.peer_addr());
},
Err(e)=>{
println!("{}:{}",&f,e);
}
}
});
stream_vec.push(jh);
}
for handle in stream_vec{
let a = handle.await;
match a{
Ok(res)=>{
},
Err(e)=>{println!("JoinErrer:{}",e);}
};
}
println!("Done");
}