diff --git a/src/netutil.rs b/src/netutil.rs index cc5810dae4ecdd..6fef1a939876ef 100644 --- a/src/netutil.rs +++ b/src/netutil.rs @@ -116,8 +116,8 @@ pub fn bind_in_range(range: (u16, u16)) -> io::Result<(u16, UdpSocket)> { let (start, end) = range; let mut tries_left = end - start; + let mut rand_port = thread_rng().gen_range(start, end); loop { - let rand_port = thread_rng().gen_range(start, end); let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), rand_port); match sock.bind(&SockAddr::from(addr)) { @@ -131,6 +131,10 @@ pub fn bind_in_range(range: (u16, u16)) -> io::Result<(u16, UdpSocket)> { } } } + rand_port += 1; + if rand_port == end { + rand_port = start; + } tries_left -= 1; } } @@ -164,8 +168,8 @@ pub fn bind_to(port: u16, reuseaddr: bool) -> io::Result { pub fn find_available_port_in_range(range: (u16, u16)) -> io::Result { let (start, end) = range; let mut tries_left = end - start; + let mut rand_port = thread_rng().gen_range(start, end); loop { - let rand_port = thread_rng().gen_range(start, end); match TcpListener::bind(SocketAddr::new( IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), rand_port, @@ -179,6 +183,10 @@ pub fn find_available_port_in_range(range: (u16, u16)) -> io::Result { } } } + rand_port += 1; + if rand_port == end { + rand_port = start; + } tries_left -= 1; } }