Skip to content

Instantly share code, notes, and snippets.

@641i130
Created November 7, 2022 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 641i130/a77495207d31670d83e9b889d66f0c4a to your computer and use it in GitHub Desktop.
Save 641i130/a77495207d31670d83e9b889d66f0c4a to your computer and use it in GitHub Desktop.
check if ip / port is up as fast as possible
[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"
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