Function zebra_test::net::random_known_port

source ·
pub fn random_known_port() -> u16
Expand description

Uses the “magic” port number that tells the operating system to choose a random unallocated port.

The OS chooses a different port each time it opens a connection or listener with this magic port number.

Creates a TcpListener to find a random unallocated port, then drops the TcpListener to close the socket.

Returns the unallocated port number.

§Usage

If you want a once-off random unallocated port, use random_unallocated_port. Don’t use this function if you don’t need to - it has a small risk of port conflicts when there is a delay between this fn call and binding the tcp listener.

Use this function when you need to use the same random port multiple times. For example: setting up both ends of a connection, or re-using the same port multiple times.

§Panics

If the OS finds no available ports

If there is an OS error when getting the socket address