You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

15 lines
290B

  1. use std::net::TcpListener;
  2. pub fn get_available_port() -> Option<u16> {
  3. (1000..9000)
  4. .find(|port| port_is_available(*port))
  5. }
  6. fn port_is_available(port: u16) -> bool {
  7. match TcpListener::bind(("127.0.0.1", port)) {
  8. Ok(_) => true,
  9. Err(_) => false,
  10. }
  11. }