Created
November 7, 2022 18:41
-
-
Save 641i130/a77495207d31670d83e9b889d66f0c4a to your computer and use it in GitHub Desktop.
check if ip / port is up as fast as possible
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "ip-up" | |
version = "0.1.0" | |
edition = "2021" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
port_check = "0.1.5" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpStream}; | |
use std::net::IpAddr; | |
use std::time::Duration; | |
fn main() { | |
use std::time::Instant; | |
let now = Instant::now(); | |
let five_seconds = Duration::new(5, 0); | |
let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 2)), 10); | |
match TcpStream::connect_timeout(&socket,five_seconds) { | |
Ok(_) => println!("Good."), | |
Err(_) => println!("Bad."), | |
} | |
// println!("{:?}",port_check::is_port_reachable("caret.rs:443")); | |
let elapsed = now.elapsed(); | |
println!("Elapsed: {:.2?}", elapsed); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment