2018-05-11 11:54:16 +00:00
|
|
|
use std::net::TcpListener;
|
|
|
|
|
2018-10-19 14:33:11 +00:00
|
|
|
pub fn get_available_port(avoid: u16) -> Option<u16> {
|
2018-10-31 07:18:57 +00:00
|
|
|
(1000..9000).find(|port| *port != avoid && port_is_available(*port))
|
2018-05-11 11:54:16 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 14:33:11 +00:00
|
|
|
pub fn port_is_available(port: u16) -> bool {
|
2019-06-06 17:49:31 +00:00
|
|
|
TcpListener::bind(("127.0.0.1", port)).is_ok()
|
2018-05-11 11:54:16 +00:00
|
|
|
}
|