aisk / rust-ping Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
Heyo, it'd be nice to have a release that avoids the failure dep, as it is now unmaintained (and potentially unsound, though unlikely to appear in practice - rust-lang-deprecated/failure#336).
Hello,
I am new to Rust and I am trying to ping multiple IP addresses at the same time using threads. When I use the ping crate sequentially, the result is as expected: IP addresses which are online yield Ok
, IP addresses which are not online yield Err
. However, when I try to ping multiple addresses concurrently, the behavior is "random", i.e., the return value from ping::ping
does not reflect whether the address is available or not.
Here is a minimal example:
use std::net::IpAddr;
use std::str::FromStr;
use std::thread;
use std::time::Duration;
use ping;
fn is_online(ip: IpAddr) -> bool {
let timeout = Duration::from_millis(50);
match ping::ping(ip, Some(timeout), None, None, None, None) {
Ok(_) => { println!("{}: online", ip); true },
Err(_) => { println!("{}: offline", ip); false },
}
}
fn main() {
let ips = [
"192.168.178.1", // is available in my network
"192.168.178.2", // is not available in my network
"192.168.178.3", // is not available in my network
"192.168.178.4", // is not available in my network
];
let mut handles = vec![];
for ip in ips {
let addr = IpAddr::from_str(ip).unwrap();
let handle = thread::spawn(move || {
is_online(addr);
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
}
In my network, only 192.168.178.1 is available, but I see this output
192.168.178.2: online
192.168.178.4: online
192.168.178.3: online
192.168.178.1: online
or also this output:
192.168.178.1: online
192.168.178.2: offline
192.168.178.4: offline
192.168.178.3: offline
I have seen the exact same problem when I was using ping with tokio and green threads.
As said, I am new to Rust and maybe the code has some inherent logic error which I don't see. So please be forgiving if this is a stupid question. :-)
I am using ping version 0.4.0 with rustc 1.67.1 (d5a82bbd2 2023-02-07).
runs in parallel, but I got the ping returns at the same time, what could be the problem
io error: Operation not permitted (os error 1)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.