tauri_api/tcp.rs
1use std::net::TcpListener;
2
3use rand::distributions::{Distribution, Uniform};
4
5/// Gets the first available port between 8000 and 9000.
6pub fn get_available_port() -> Option<u16> {
7 let mut rng = rand::thread_rng();
8 let die = Uniform::from(8000..9000);
9
10 for _i in 0..100 {
11 let port = die.sample(&mut rng);
12 if port_is_available(port) {
13 return Some(port);
14 }
15 }
16 None
17}
18
19/// Checks if the given port is available to use.
20pub fn port_is_available(port: u16) -> bool {
21 TcpListener::bind(("127.0.0.1", port)).is_ok()
22}