bparli / port-scanner

Simple, yet fast, async port scanner library for Rust
MIT License
10 stars 0 forks source link

Details for rust beginners #2

Open hanckmann opened 4 years ago

hanckmann commented 4 years ago

Hi, This looks like a good lib and I am trying to use it. Your examples are useful, but it would be good to also hint on how to really use it.

my main.rs

use std::net::SocketAddr;
use std::time::Duration;
use async_std::task;
use futures::future::join_all;
use async_port_scanner::Scanner;

fn main() {
  let ps = Scanner::new(Duration::from_secs(4));

  let my_ftr = ps.run_batched("127.0.0.1".to_string(), 1, 65535, 3000);
  let dev1_ftr = ps.run_batched("192.168.2.220".to_string(), 1, 65535, 3000);
  let all_ftrs = vec![my_ftr, dev1_ftr];
  let results: Vec<Vec<SocketAddr>> = task::block_on(async move { join_all(all_ftrs).await });
  println!("{:?}", results);
}

My Cargo.toml dependencies:

[dependencies]
async-port-scanner = "0.1.3"
futures = "0.3"
async-std = "1.5"

However, this results in the error:

cargo run
   Compiling rnetworkview v0.1.0 (/.../Projects/rnetworkview)
    Finished dev [unoptimized + debuginfo] target(s) in 2.00s
     Running `target/debug/rnetworkview`
thread 'main' panicked at 'cannot run an executor inside another executor', <::std::macros::panic macros>:2:4
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
bparli commented 4 years ago

Hey @hanckmann, thanks for the issue. You weren't doing anything wrong, it looks like futures 0.3.6 became more explicit about whats not allowed. I was able to push a patch (0.1.4) which should resolve that error for you. I also bumped async-std to the next version. Your Cargo.toml would look like this:

[dependencies]
async-port-scanner = "0.1"
futures = "0.3"
async-std = "1.6"

Existing code should work fine as is if you want to give it another try